Exam 70-553 - Read, write, and validate XML by using the XmlReader class and the XmlWriter class.

Section 3

  • Part 2
    • Topic 3

Read, write, and validate XML by using the XmlReader class and the XmlWriter class.

  • Read XML data by using the XmlReader class.
  • Read all XML element and attribute content.
  • Read specific element and attribute content.
  • Read XML data by using the XmlTextReader class.
  • Read node trees by using the XmlNodeReader class.
  • Validate XML data by using the XmlValidatingReader class.
  • Write XML data by using the XmlWriter class.


New to .Net 2.0 is the ability to read XML directly with the XmlReader and the XmlWriter Class. In 1.1, these classes were abstract, but they now have a static Create method that can be used to create an instance. This create method accepts a file path or stream as well as a XmlReaderSettings or XmlWriterSettings instance.

To Read all Xml Elements Content, use the ReadString method, which returns the contents of the xml nodes. Use the ReadStartElement or ReadEndElement to navigate to a specific node. These methods both call the MoveToContent Method to navigate to the specific element. To get at the attributes of the node, use methods like HasAttributes, AttributeCount or the MoveToNextAttribute Method to navigate the Xml.

Using the XmlTextReader has not changed. You initialize an instance of the XmlTextReader by passing the filename and path to the constructor. You can then loop through the nodes with the Read method, inspecting each Nodes type to determine if it is an elment, attribute or value among other types.

The XmlNodeReader can be used to navigate through a XmlDocument or any XmlNode. The XmlValidatingReader can be used to validate Xml documents and fragments using Schemas or XmlResolvers. See the resources article for more details.

Other Resources & Links:

Reading and Writing XML in .NET Version 2.0

Using the XmlReader Class

How to read XML from a file by using Visual C# 2005 or Visual C# .NET

Reading Node Trees with XmlNodeReader

Validation of XML with XmlValidatingReader

Exam 70-553 - Create, configure, and customize user assistance controls and components.

Exam 70-553 - Manage connections and transactions.