Reader Level:

Microsoft .NET and XML

By Mahesh Chand on Nov 16 2009
In this article I will explain you about Microsoft .NET and XML.
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 10.6k
  • 0

This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".

Microsoft's .NET Framework utilizes XML features to internally and externally transfer data between applications. In this section, you'll see XML namespaces and classes, which I'll be using in the examples through out this article. In the .NET Framework Library, the System.Xml and its four supportive 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.

Before moving to the next topic, I'll describe these namespaces and their classes. I'll also discuss some of these classes in more detail through out this article.

The System.Xml Namespace

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), DOM Level 2 core and SOAP 1.1.

The following sections define some of the System.Xml namespace classes.

The xml Node Class

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.

Document classes

The System.Xml namespace also contains classes to a deal with XML documents. The XmlDocument and XmlDocument Fragment classes represent an entire XML document and a fragment of a document, respectively. The XmlDocumentFragment class is useful when you deal 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 form XmlNode. Besides the methods contained in XmlNode, this class implements a series of Createxxx methods to create a document's contents such as Comment, Element, Text and all the other contents discussed in the "DOM Overview" section of this article. You can even load an XML document by using its Load and LoadXml methods.

Each content type of an XML document has 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 Attribute and XmlComment classes represent an attribute and comment of a document. You'll see these classes in the examples.

Reader and write classes

Six classes (XmlReader, XmlWriter, XmlTextWriter, XmlTextReader XmlValidatingReader, and XmlNodeReader) represent the reading and writing XML documents.

XmlReader and XmlWriter are abstract base classes representing a reader that provides fast, non-cached, forward-only stream access to XML documents. XmlReader has three classes: XmlTextReader, XmlValidatingReader, and XmlNodeReader. As their node imply, XmlTextReader is for reading text XML documents, XmlNodeReader is for reading XML DOM trees, and XmlvalidatingReader can validate data using DTDs or schemas. This reader also expands general entities and supports default attributes. Xml writer is an abstract base class that defines functionality to write XML. It implements methods and properties to write XML contents. XmlTextWriter class comes from the XmlWrinter class.

Other classes

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.

  • XmlException defines functionality to represent detailed exceptions
  • XmlNamespaceManager resolves, Adds, and removes namespace to a collection 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

The System.Xml.Schema namespace contains classes to work with XML schemas. These classes support XML schemas for structure 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, XmlSchemaPath, and XmlSchemaType.

The System.Xml.Serialization Namespace

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 document. Again this namespace also defines many classes. The discussion of these classes is beyond the scope of this article.

The System.Xml.XPath Namespace

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 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 reads data and treats a document as a tree and provides methods to traverse through a document as a 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 System.Xml.Xsl Namespace

The last namespace, System.Xml. Xsl, defines functionality for XSL/T transformations. It suppports XSLT 1.0. The XsltTransform class defines functionality to transform data using an XSLT stylesheet.

DOM Interfaces

As you've seen in the previous discussion, you can represent an XML document in a tree structure using DOM interfaces and objects (shown in figure 6.3).

Microsoft .NET provides a nice wrapper around these interfaces: the DOM 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 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.

XML .NET Architecture

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 XmlRader and XmlWriter classes are abstrct base classes that provide fast, non-cached, forward-only cursors to read/ 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.4 shows a relationship between these classes and the XML.NET architecture.


Figure 6.4: XML.NET architecture

The System.Xml.Xsl interface provides classes that implement XSLT. (I'll discuss XSLT in more detail later in this article.) 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. I'll discuss these classes in "Navigation in XML" and other sections of this article.

Adding System.Xml Namespace Reference

You're probably aware of this, but before using System.Xml classes in your application, you may need to add a reference to the System.Xml.dll assembly using Project > Add Reference (see figure 6.5) and include the System.Xml namespace:

using System.Xml;


Figure 6.5: Adding a reference to the System.Xml.dll assembly

The abstract base classes XmlReader and XmlWriter support reading and writing XML documents in the .NET Framework.

Hope this article would have helped you in understanding Microsoft .NET and XML. See other articles on the website also for further reference.

adobook.jpg This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.