O'Reilly logo

C# 5.0 Programmer's Reference by Rod Stephens

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required