System.Xml
XML is now the universal standard for data interchange between applications systems. XML is not some geeky add-on required in certain obscure cases, but rather a universal requirement of any networked application. Therefore, easy and efficient access to XML by applications is a central requirement of any operating system, and the .NET Framework contains many classes for the easy handling of XML.
The abstract base class XmlReader defines methods for forward-only, read-only access XML documents and streams. The class XmlTextReader derives from XmlReader and provides a basic implementation of its logic. It checks a document for being well-formed, but does not validate it against a DTD or schema. It does not read an entire document into memory at a time, as does a DOM parser. Like a SAX parser, it reads a document one node at a time. However, unlike the way a SAX parser pushes data into its client application, the client application must pull data from the XmlTextReader.
The .NET Framework provides conceptually symmetrical classes for writing an XML document or stream. The abstract base class XmlWriter defines methods for fast, non-cached, forward-only writing of XML documents and streams. The class XmlTextWriter derives from XmlWriter and provides a basic implementation of its logic.
The XmlReader and XmlWriter operate on one XML node at a time, advancing through the document in response to calls from the client. They do not return a separate object representing this node, as does a DOM parser. Instead, the properties of the current node (Name, NodeType, Value, etc.) are properties of the XmlReader and XmlWriter themselves.
Despite operating on only one node at a time, an XmlReader-derived object is required to contain a certain amount of per-document data in order to do its job. For example, it can tell you the namespace URI or prefix of the current node, which requires that it remember all of the namespaces and prefixes declared in the document. The .NET Framework contains utility classes to help an implementer provide these capabilities. The class XmlTextReader uses them internally, and so can anyone else.
The class XmlParserContext is a scratchpad object used by a reader. It is intended as a holder, a one-stop shopping location, for all of the per-document information required by the reader. It has properties of its own, such as DocTypeName for the document type declaration and Encoding for the character encoding type used by the document. It also holds utility objects for accessing additional properties.
The XmlParserContext contains an object of class XmlNamespaceManager, a utility class that manages namespaces and their prefixes. This class provides methods that map prefixes onto namespace URIs and vice versa. It also contains a stack for managing the current default namespace scope. Several of the XmlTextReader properties, such as NamespaceURI, map through the XmlParserContext into its XmlNamespaceManager.
The XmlParserContext class also contains a table of names. Since XML element and attribute names are often long and descriptive, so as to have meaning to human designers, storing the string name for every node can consume large amounts of memory. Since the same element and attribute names often repeat many times within an XML document, it makes sense to store each human-meaningful name only once in a table, and use shorter "atomized strings" to refer to each occurrence of it. The abstract base class XmlNameTable defines the methods and properties used for this process, and the derived class NameTable provides a default implementation.
The parsing process of an XML document often requires the parser to include other specified documents. For example, the !DOCTYPE statement pulls in a DTD, xsd:include pulls in a schema, and xsl:include pulls in a style sheet. The abstract base class XmlResolver contains method definitions for an object that performs this function. The class XmlUrlResolver derives from it, providing a default implementation that resolves the inclusion based on a URL.
The class XmlConvert is a utility class that contains methods for frequently used data conversions in the XML space. It can convert names that are legal in other applications (for example, names containing spaces) into names that are legal in XML. It also contains methods for converting XML element or attribute values, which are always strings in an XML document, into CLR data types. It is very handy when constructing higher levels of abstraction that automatically convert XML data values into the types specified by a schema before presenting them to a client application.
As with all .NET Framework classes, the ones in this namespace signal errors by throwing exceptions. The class XmlException contains the standard exception data, plus additional XML-specific properties such as the line number and column number in the file at which the error occurred.
|