Avalidating parser can use a DTD or schema to verify that a document is properly constructed accord- ing to the rules for the XML application, and it is supposed to complain loudly if the rules aren’t followed. A DTD or XML schema can also specify default values for the attributes of various elements, and a validating parser can fill them in when it encounters elements with no attributes listed. This capability can be important when you are processing XML documents that you have received from the outside world. For example, if vendors send XML-marked invoices to your company, you’ll want to ensure that they contain the right elements in the right order. |
Anon-validating parser only requires that the document be well-formed. Because of the design of XML, it’s possible to parse well-formed documents without referring to a DTD or XSD schema. Non-validating parsers are simpler, and many of the free parsers available over the Web are non-validating. They are usually sufficient for processing XML documents generated within the same organization or documents whose validity constraints are so complex that they can’t be expressed by a DTD and need to be verified by application logic instead. |
XML Parsing Support in .NET Framework |
Implementing XML with the .NET Framework class library requires referencing the System.Xml.dll |
assembly. The .NET Framework class library provides two ways of parsing XML data: |
Fast, non-cached, forward-only access |
Random access via an in-memory DOM tree |
Both methods of processing XML data are equally valid; however, each has a definite time when it is better suited. At other times, both work equally well, and the decision of which to use is up to the developer’s taste. The next sections explore both of these methods in detail. |
Forward-Only Access |
Forward-only access to XML is amazingly fast. If you can live with the restriction that you can process the XML data only in a forward-only method, this is the way to go. The core class for implementing this method of read-only, forward-only access is named System.Xml.XmlReader. The XmlReader class allows you to access XML data from a stream or XML document. The XmlReader class conforms to the W3C XML 1.0 and the Namespaces in XML recommendations. |
In .NET 1.x, the XmlReader class is an abstract class and provides methods that are implemented by the derived classes to provide access to the elements and attributes of XML data. With .NET Framework 2.0, however, the Create() method of the |
XmlReader class returns an instance of the XmlReader object that you can directly use to read an XML document. |