Appendix V XML

This appendix summarizes useful XML topics and techniques.

Special Characters

The following table lists five special characters defined for use in XML files.

CharacterCode
<<
>>
&&
''
""

You can also include special characters inside CDATA sections. A CDATA section begins with <![CDATA[ and includes all the following text until it reaches the closing sequence ]]>. The CDATA can include carriage returns, ampersands, quotes, and other special characters.

Writing XML Data

The .NET Framework provides two main ways to write XML data: the XmlWriter class and the XML Document Object Model. The following two sections describe these approaches.

XmlWriter

The XmlWriter class provides methods for writing the pieces of an XML file. To use an XmlWriter to write XML data into a file, call the class’s Create method to create the file. Then use the other methods to write the pieces of the XML document into the file.

The following table lists the most useful XmlWriter methods.

MethodPurpose
CloseCloses the writer’s underlying stream.
CreateCreates an XmlWriter associated with a file, stream, StringBuilder, or other object.
DisposeFrees the writer’s resources. (You can use the using statement to ensure that the writer is disposed.)
FlushFlushes output to the underlying stream.
WriteAttributeStringWrites an attribute with a specified name and value.
WriteCDataWrites CDATA.
WriteCommentWrites a comment.
WriteElementStringWrites an element with a specified name ...

Get C# 5.0 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.