The programming world is moving more and more toward the Web, and Extensible Markup Language (XML) is an essential part of Web-based programming. This chapter assumes you have an understanding of basic XML syntaxes and documents.
This chapter introduces the classes provided by the .NET Framework Library to work with XML documents. In this chapter, we discuss how to read from and write to XML documents using the .NET Framework Library classes. Then we discuss how to navigate through XML documents. We also discuss XML transformations. Furthermore, this chapter covers the relationship between ADO.NET and XML and shows how to mix them up and use rich ADO.NET database components to display and manipulate XML data. Finally, we cover the XPathNavigator class, which navigates XML documents.
Microsoft's .NET Framework utilizes XML features both internally and externally to transfer data between applications. In the following sections, you'll learn about the XML namespaces and classes, which you'll use in the examples throughout this chapter.
In the .NET Framework Library, the System.Xml and its four supporting namespaces define the functionality to work with XML data and documents. These namespaces are System.Xml, System.Xml.Schema, System.Xml.Serialization, System.Xml.XPath, and System.Xml.Xsl. These namespaces reside in the System.Xml.dll assembly.
Covering every class defined in System.Xml and its related namespaces is impossible in this chapter. We cover the commonly used classes. If you want to learn more about XML in .NET, you can find many useful source code samples, articles, and tutorials on C# Corner's XML.NET section (www.c-sharpcorner.com/xmlnet.asp).
The System.Xml namespace defines common and major XML functionality. It defines classes for XML 1.0, XML namespaces and schemas, XPath, XSL Transformations (XSLT), Document Object Model (DOM) Level 2 Core, and Simple Object Access Protocol (SOAP) 1.1.
The following sections define some of the System.Xml namespace classes.
The XmlNode class, an abstract base class for XmlDocument and XmlDataDocument, represents a single node in a document. This class implements methods for adding, removing, and inserting nodes into a document. This class also implements properties to get data from a node such as name, child nodes, siblings, parents, and so on.
The System.Xml namespace also contains classes to deal with XML documents. The XmlDocument and XmlDocumentFragment classes represent an entire XML document and a fragment of document, respectively. The XmlDocumentFragment class is useful when you're dealing with a small fragment of a document.
The XmlDataDocument class allows you to work with relational data using the DataSet object. It provides functionality to store, retrieve, and manipulate data. The XmlDocumentType class represents the type of document.
The XmlDocument and XmlDataDocument classes come from XmlNode. Besides the methods contained in XmlNode, this class implements a series of CreateXXX methods to create a document's contents such as comments, elements, text, and so on. You can even load an XML document by using its Load and LoadXml methods.
Each content type of an XML document has a corresponding class defined in this namespace. The classes are XmlAttribute, XmlCDataSection, XmlComment, XmlDeclaration, XmlEntity, XmlEntityReference, XmlProcessingInstruction, XmlText, and XmlWhitespace. All of these classes are self-explanatory. For example, the XmlAttribute and XmlComment classes represent an attribute and comment of a document. You'll see these classes throughout the examples in this chapter.
Six classes (XmlReader, XmlWriter, XmlTextWriter, XmlTextReader, XmlValidatingReader, and XmlNodeReader) represent the reading and writing of XML documents.
The XmlReader and XmlWriter classes are abstract base classes that represent a reader that provides fast, non-cached, forward-only stream access to XML documents. XmlReader has three classes: XmlTextReader, XmlValidatingReader, and XmlNodeReader. As their names imply, XmlTextReader reads text XML documents, XmlNodeReader reads XML DOM trees, and XmlValidatingReader validates data using Document Type Definitions (DTDs) or schemas. This reader also expands general entities and supports default attributes. XmlWriter is an abstract base class that defines functionality to write XML. It implements methods and properties to write XML contents. The XmlTextWriter class comes from the XmlWriter class.
The XmlConvert class provides conversion in XML. It defines methods for converting Common Language Runtime (CLR), or .NET data types, and XML Schema Definition (XSD) types. It contains the following classes:
XmlException defines functionality to represent detailed exceptions.
XmlNamespaceManager resolves, adds, and removes namespaces and provides scope management for these namespaces.
XmlLinkedNode returns the node immediately preceding or following this node.
XmlNodeList represents a collection of nodes.
The System.Xml.Schema namespace contains classes to work with XML schemas. These classes support XML schemas for structures and XML schemas for data types.
This namespace defines many classes to work with schemas. The discussion of these classes is beyond the scope of this book. Some of these namespace classes are XmlSchema, XmlSchemaAll, XmlSchemaXPath, and XmlSchemaType.
This namespace contains classes to serialize objects into XML format documents or streams.
Serialization is the process of reading and writing an object to or from a persistent storage medium such as a hard drive.
You can use the main class, XmlSerializer, with TextWriter or XmlWriter to write the data to a document. Again, this namespace also defines many classes. The discussion of these classes is beyond the scope of this chapter.
This namespace is pretty small in comparison to the previous three namespaces. This namespace contains only four classes: XPathDocument, XPathExression, XPathNavigator, and XPathNodeIterator.
The XPathDocument class provides fast XML document processing using the stylesheet language of XML: XSLT. This class is optimized for XSLT processing and the XPath data model. The CreateNavigator method of this class creates an instance of XPathNavigator.
The XPathNavigator class treats an XML document as a tree and provides methods to traverse through a document as tree. Its MoveXXX methods let you traverse through a document.
Two other classes of this namespace are XPathExpression and XPathIterator. XPathExpression encapsulates an XPath expression, and XPathIterator provides an iterator over the set of selected nodes.
The last namespace, System.Xml.Xsl, defines functionality for XSLT transformations. It supports XSLT 1.0. The XsltTransform class defines functionality to transform data using an XSLT stylesheet.
You can represent an XML document in a tree structure using DOM interfaces and objects.
Microsoft .NET provides a nice wrapper around these interfaces: the DOM Application Programming Interface (API). This wrapper has a class for almost every interface. These classes hide all the complexity of interface programming and provide a high-level programming model for developers. For example, the .NET class XmlDocument provides a wrapper for the Document interface.
Besides the DOM, the Microsoft .NET XML API also provides corresponding classes for the XPath, XSD, and XSLT industry standards. These classes are well coupled with the .NET database models (ADO.NET) to interact with databases.
The XML .NET API is a nice wrapper around the XML DOM interfaces and provides a higher-level of programming over XML documents. The heart of the XML .NET architecture consists of three classes: XmlDocument, XmlReader, and XmlWriter. The XmlReader and XmlWriter classes are abstract base classes that provide fast, non-cached, forward-only cursors to read and write XML data. XmlTextReader, XmlValidatingReader, and XmlNodeReader are concrete implementations of the XmlReader class. The XmlWriter and XmlNodeWriter classes come from the XmlWriter class. XmlDocument represents an XML document in a tree structure with the help of the XmlNode, XmlElement, and XmlAttribute classes.
Figure 6-1 shows a relationship between these classes and the XML .NET architecture.
Figure 6-1: XML .NET architecture
The System.Xml.Xsl interface provides classes that implement XSLT. The XmlTransform class implements XSLT. This class reads and writes XML data with the help of the XmlReader and XmlWriter classes.
The XPathDocument and the XPathNavigator classes provide read/write and navigation access to the XML documents.
Associated with these classes are some more powerful classes for working with XML. We discuss these classes in "Navigating in XML" and other sections of this chapter.
Before using the System.Xml classes in your application, you may need to add a reference to the System.Xml.dll assembly and include the System.Xml namespace in your application. You can add a reference to the System.Xml.dll assembly by selecting Project Add Reference, choosing the .NET tab, and selecting System.Xml.dll from the component name listing. After adding the reference, you need to import the namespace by adding the following line:
The abstract base classes XmlReader and XmlWriter support reading and writing XML documents in the .NET Framework.