Formatting the XmlTextWriter s Output


Formatting the XmlTextWriter 's Output

Before starting to write XML data, you should consider whether and how the XML should be formatted. Formatting can make your XML data appear more organized and more readable to the human eye.

Formatting is controlled by the Formatting property of the XmlTextWriter . The Formatting property accepts an enumeration of the type System.Xml.Formatting . The enumeration has two values: Indented and None . None is the default value, meaning that no formatting is performed by a newly created XmlTextWriter . Setting the Formatting property to Formatting.Indented means that child elements will be indented.

You can specify which character to use for indention as well as how many of those characters equal one indention. This is controlled by the IndentChar and Indention properties, respectively. The IndentChar property provides read and write access to the character used for indenting. The default value is the space character. To ensure valid XML, the property can be set to only a valid white space character: 0x9 , 0x10 , 0x13 , or 0x20 . The Indention property provides read and write access to how many IndentChars are written for each indentation. The default value is two. The XmlTextWriter will throw a System.ArgumentException if you attempt to change the Indention property to a negative value.

All three formatting properties can be set at any time, and they will be applied the next time an indention is inserted into the document. The IndentChar and Indention properties remain set throughout the life of the reader or until they are changed to new values. Setting the Formatting property to Formatting.None will not reset the IndentChars and Indention properties back to their defaults.

Turning formatting on will increase the size of your XML files and may cause performance degradation in the XmlTextWriter . When deciding whether to use formatting, you should determine if the XML needs to be human-readable . If not, it should not be formatted. You could use formatted XML while developing and debugging your application, but use unformatted XML for production code. The bottom line is that formatting XML is for humans . Formatting does not benefit XML parsers.



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net