20.2 XML and .NET
Because of its extensible nature, XML can be used for a variety of purposes. Modern applications use XML as a messaging protocol, as a transport protocol, and even for backend data storage. The .NET platform uses XML as its key technology in ASP.NET, Web services, and so on. The .NET Framework XML classes are also tightly coupled with Managed Data Access (ADO.NET). Traditionally, there have always been different programming models for working with relational versus hierarchical data. The .NET platform, however, allows for transparent translation of relational data to XML and vice versa. This makes it possible to retrieve a relational ADO.NET dataset and walk it using the DOM, query or navigate it using XPath 1.0, and transform it using XSLT 1.0.
As mentioned earlier, the primary language-neutral APIs for parsing XML documents are the DOM and the SAX API. The DOM API stores the XML document in a tree data structure, thereby allowing complete navigation of all the elements (in no particular order). Users can modify the DOM tree. This API is very convenient, but it can consume a lot of memory when you're working with large documents.
The SAX API was developed to counter the defects of the DOM API. The SAX API uses an event-driven model in which parsing every character of the XML document is handled through event listeners. The parser parses the document in only one direction. The SAX API puts the onus of handling the state of the document parsing on the end user, and therefore it is inherently difficult and awkward to use.
The .NET XML classes support both DOM and SAX-like APIs. The SAX-like API in .NET is an improvement over the traditional SAX model, in the sense that it gives a lot more control to the end user during the parsing process. The so-called pull model used by .NET's version of SAX allows programmers to skip sections of the document, process only certain elements of the document, and interact a lot more with the document. You can say that .NET's SAX sits between DOM and pure SAX. It mixes the convenience of DOM with the power of the pure SAX API.
The .NET XML classes are spread over the namespaces given in Table 20.1.
Table 20.1 .NET XML Namespaces
System.Xml |
Contains the core classes required for eading and writing XML documents. |
System.Xml.Schema
|
Contains the XML classes that provide standards-based support for XML Schema Definition Language (XSD) schemas. The supported standards are as follows:
|
System.Xml.Serialization |
Contains classes that are used to serialize and deserialize objects into XML format documents or streams. |
System.Xml.XPath |
Contains the XPath parser and evaluation engine. It supports the W3C XML Path Language (XPath) Version 1.0 Recommendation (http://www.w3.org/TR/xpath). |
System.Xml.Xsl |
Provides support for XSLT transforms. Also supports the W3C XSL Transformations |
We will look at the classes in the System.Xml and System.Xml.XPath namespaces.