Recipe 14.24. Validating an XML Document


Problem

You have an XML document that is supposed to adhere to a specific schema. How can you be sure the document is valid?

Solution

There are a variety of XML validation methods, including DTD and both internal and external Schema definitions. If you are going to read the XML content into a System.Xml.XmlDocument object, you can verify it as it is read using any of these validation methods. Normally, an XmlReader reads any valid XML into an XmlDocument object without validation. However, you can indicate the type of validation to perform by setting the various properties of an XmlReaderSettings object and using it when creating the XmlReader. Here is the basic code used to process XML with custom settings:

 ' ----- XML file contained in 'xmlFileName' variable. Dim readContent As Xml.XmlReader Dim xmlContent As Xml.XmlDocument Dim customSettings As New Xml.XmlReaderSettings ' ----- Modify customSettings properties here, then… readContent = Xml.XmlReader.Create(xmlFileName, customSettings) xmlContent = New Xml.XmlDocument xmlContent.Load(readContent) 

The code you add in the "Modify customSettings" area of the code depends on the type of verification or processing you wish to do. Include the following statements to validate the XML using a known external schema (.xsd) file:

 customSettings.ValidationType = Xml.ValidationType.Schema customSettings.Schemas.Add("urn:my-schema", "MySchema.xsd") 

Discussion

The XmlReaderSettings class includes features that control the processing of XML content during import, including the handling of whitespace and embedded comments. It also determines how to handle validation through its ValidationType property. In Visual Basic 2005, the allowed settings include None (for no validation, the default), DTD (for included DTD content), and Schema (for XSD processing, either internal or external).

Care must be taken when performing DTD validation because malformed DTD entries can cause processing issues. Because of this, DTD processing is disabled by default. To enable it, you must alter two settings:

 customSettings.ValidationType = Xml.ValidationType.DTD customSettings.ProhibitDtd = False 

If your XML content includes an XSD schema within the XML content (i.e., an inline schema), you must enable processing support:

 customSettings.ValidationType = Xml.ValidationType.Schema customSettings.ValidationFlags = _    customSettings.ValidationFlags Or _    Xml.Schema.XmlSchemaValidationFlags.ProcessInlineSchema 

When you validate XML, any content that deviates from the schema raises exceptions (System.Xml.XmlException) that emanate from the call to XmlDocument.Load( ). You can also capture problems through a ValidationEventHandler event, exposed by the XmlReaderSettings class.

See Also

Recipes 14.22 and 14.23 discuss other methods of working with XML content.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net