A lot of people associate the .NET framework with XML, and for good reason. .NET uses XML behind the scenes to implement many of its development tools, such as SOAP and Web services. Beyond that, however, .NET provides a powerful set of classes for working with XML directly. Whatever you need to do with XML—sequential or random access, validation, transforms, or output—the .NET Framework provides you with tools that are not only powerful but easy to use.
This article from InformIT provides an overview of the most important of these classes. All of .NET's XML classes are in the System.XML namespace, and support the following standards (listed with their WWW namespaces):
- XML 1.0 including DTDs
- XML Namespaces, both stream-level and DOM
- XSD Schemas
- XPath expressions
- XSLT transformations
- DOM Level 1 Core
- DOM Level 2 Core
The XmlTextReader class provides non-cached, forward-only access to a stream of XML data. It is designed specifically for fast access to XML data while placing minimal demands on the system's resources. Functionally, XmlTextReader is similar to the Simple API for XML (SAX), another technique for reading XML that is popular with non-.NET programmers.
The XmlValidatingReader class, as its name implies, provides data validation capabilities. Specifically, it can validate XML data against a document type definition (DTD), an XML schema definition language (XSD) schema, or an XML Data Reduced (XDR) schema. This class does not work alone; it must be used in conjunction with an instance of XmlTextReader that is passed to the constructor. Thus, this class gives you the forward-only capabilities of XmlTextReader with validation added. XmlValidatingReader also adds support for default attributes and the ability to resolve external references. Validation of XML data is an inherently complex and slow process.
The XmlDocument class implements the W3C Document Object Model (DOM) core levels 1 and 2. This class provides random, cached access to the XML data. In other words, the data is held in memory and your program can move forward and backward as needed. Actually, "forward" and "backward" are not really accurate, because the DOM represents XML data as a tree of nodes, so what you are really doing is "walking the tree." The XmlDocument class also permits you to modify the document's data and structure.
Other Classes in System.XML
We've looked at three of the most important XML-related .Net classes, but there are many more that cannot be covered here. For example, the XmlTextWriter class lets you create XML output that conforms to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations. And the XslTransform class transforms XML data using an XSLT stylesheet.
XML is becoming increasingly important as a data storage and transfer standard in many areas of information technology. With the tools provided by the .NET Framework, you should be able to handle essentially any XML-related programming task.