Microsoft is serious about .NET’s commitment to XML. This is made obvious by the extent to which XML is used in the .NET architecture and supported through several feature-rich namespaces. In this chapter, you are introduced to the XML API in the .NET Framework. Before looking at the XML support in the .NET Framework, it is important to examine the design goals of .NET Framework 2.0. |
Design Goals for XML Support in .NET Framework 2.0 |
Through the XML namespaces and classes present in the .NET Framework 2.0 base class library, you can easily build XML support into your applications. These classes enable you to read, write, manipulate, and transform XML. Because XML manipulation is inevitable in application development, it is recommended that all developers have an understanding of these core XML classes. When the XML team in Microsoft started designing the XML feature set for the .NET Framework 2.0, they had the following design goals in mind: |
Chapter 3 |
Better Standards compliance —Support for the major W3C XML standards that provide cross- |
platform interoperability, such as XML 1.0, XML Namespaces 1.0, XSLT 1.0, XPath 1.0, and W3C XML schema 1.0. |
Usability— XML API should be not only easy-to-use but also intuitive. |
Seamless Integration with ADO.NET —The classes in the XML API can really be considered |
part of ADO.NET as an XML data access API. The combination of System.Data.DataSet and the System.Xml.XmlDataDocument classes provide a seamless experience when moving between XML and relational data. |
Significant Performance Improvements — This was the number one requirement for the .NET |
Framework 2.0 release. The new XSLT processor through the introduction of the new System .Xml.Xsl.XslCompiledTransform class is one of the many performance improvements with XML API in .NET Framework 2.0. |
Developer Productivity enhancements — These enhancements are geared towards increasing |
the productivity of the developers by allowing them to perform common tasks even easier to do in less lines of code. |
Support for Strong Types and XML schema —In the .NET Framework 1.x, almost all of the XML |
API were untyped in that the data was both stored and retrieved as string types. This is enhanced in .NET Framework 2.0 by integrating schema information deeply across the XML namespaces. This provides for more efficient storage, improved performance, and better inte- gration with the .NET programming languages. |
XML Namespaces |
XML API in .NET Framework 2.0 is mainly encapsulated in five namespaces. These namespaces house all of the XML functionality within the .NET Framework class library. Table 3-1 describes these name- spaces at a high level. |
Table 3-1. XML Namespaces in .NET Framework 2.0 |
Namespace |
Description |
Contains the classes that provide the core of all XML functionality |
System.Xml |
System.Xml.Schema Provides support for XML Schema Definition Language |
(XSD) schemas |
System.Xml.Serialization Provides classes that allow you to serialize and deserialize |
objects into XML formatted documents |
System.Xml.XPath Provides support for the XPath parser and evaluation |
functionality |
System.Xml.Xsl Provides support for XSLT transformations |
The next few sections provide an overview of the classes and functionalities contained in these namespaces. |
06_596772 ch03.qxd 12/13/05 11:10 PM Page 43 |
XML Classes in the .NET Framework |
The System.Xml Namespace |
The classes in the System.Xml namespace are designed to fully support your XML needs. Your needs may range from reading and writing XML to storing XML. In fact, your application’s needs may even extend to querying XML or transforming XML. There are many feature-rich classes available in this namespace that provide reading, writing, and manipulating XML documents. |
The System.Xml.Schema Namespace |
This namespace offers classes, delegates, and enumerations used to support your XSD language needs. It strongly supports the W3C Recommendations for XML schemas for structures and XML schemas for data types. The classes in this namespace service the Schema Object Model (SOM). |
The System.Xml.XPath Namespace |
This namespace offers support for the XPath parser (query support) via several classes, interfaces, and enumerations. Two commonly used classes from this namespace are XPathDocument (fast, read-only cache, optimized for XSLT) and XPathNavigator (editable, random access, cursor model). Note that the |
XPathNavigator class can now be used to edit XML data in addition to providing a cursor model for navigating XML data. |
The System.Xml.Xsl Namespace |
This namespace provides full support for the Extensible Stylesheet Transformation (XSLT) technology. Although several classes and interfaces are offered in this namespace, you will likely use the |
XslCompiledTransform class and XsltArgumentList classes most often. |
The System.Xml.Serialization Namespace |
This namespace offers classes and delegates to assist with object serialization. Among the many managed classes offered, you will use the XmlSerializer class the most. Using the XmlSerializer class, you can serialize and deserialize instantiated objects to and from XML documents or streams. Object serialization is a useful technique for persisting (or saving) the state of an object so that you can later re-create an exact copy of the object. Object serialization is also useful when you want to pass the object (marshal by value) during .NET Remoting scenarios. |
The preceding list of namespaces is provided to give you a more complete picture of the XML support available through the .NET Framework and platform. Combining this information with that of the classes in the System.Xml namespace, you are certainly off to an informed start. Now that you have understood the different XML namespaces, you are ready to examine the different XML-related capabilities such as XML Parsing, XML Validation, XPath, XML Serialization, XML Web Services, and so on, and how they are supported in .NET Framework 2.0. The next section examines how XML is enabled in .NET Framework. |
XML Parsing |
Parsing is not always as simple as just reading through an XML document and verifying it for ASCII text. The structure and rules of your governing DTD, or XSD schemas can be verified when processing these instance documents if you utilize a validating parser. You need this parsing application to evaluate the instance document and determine if it’s valid and then make it available for secondary applications to utilize the data contained therein. |