Chapter 3. XML Data Validation

The base XML reader examined in Chapter 2—the XmlTextReader class—does not enable you to validate the contents of an XML source against a schema. The correctness of XML documents can be measured using two distinct and complementary metrics: the well-formedness of the document and the validity. Well-formedness of the document refers to the overall syntax of the document. Validation applies at a deeper level and involves the semantics of the document, which must be compliant with a user-defined layout.

The XmlTextReader class ensures only that the document being processed is syntactically correct. By design, the XmlTextReader class deliberately avoids making a more advanced analysis of the nodes in the document and checking their internal dependencies. A more specialized class is available in the Microsoft .NET Framework for accomplishing this more complex task—the XmlValidatingReader class. This chapter will focus on techniques and classes available in the .NET Framework to perform validation on XML data.

Although validation is a key aspect in projects that involve critical document exchange across heterogeneous platforms, it does come at a price. Validating a document means taking a while to analyze the constituent nodes; the number, type, and values of their attributes; and the node-to-node dependencies. When applications handle a fully validated document, they can be certain not only about the overall syntax but even about the contents. In a normal XML document, a node simply represents itself—a rather generic repository of hierarchical information. In a validated XML document, on the other hand, the same node to the application’s eye represents a strongly typed and strongly defined piece of information. Basically, in a validated document, a node <invoice_number> ceases to be a node and becomes what it was intended to be—the number of the invoice.

Clearly, a nonvalidating reader (and, more generally, a nonvalidating XML parser) will run faster than a validating reader, and that’s why XML parsers usually provide XML validation as an option that can be programmatically toggled on and off. In .NET applications, you use XmlTextReader if you simply need well-formedness; you resort to XmlValidatingReader if you need to validate the schema of the document.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.252.23