Start Validating documents

Validating documents

The following example validates the file as it is loaded into the Xml Document object by creating the Xml Document object using a validating Xml Reader object.

Create("contoso Books.xml", settings) Dim document As Xml Document = New Xml Document() document.

For more information about read-only and editable XPath Navigator objects, see the Reading XML Data using XPath Document and Xml Document topic.

The XPath Document object validates the XML document as it is loaded, but because you cannot edit the XML data in the XPath Document object, you cannot revalidate the XML document.

In the case of validation, you can create an XPath Document object just like you create an Xml Document object using a validating Xml Reader object as described earlier in this topic.

The Doc Book schema is a precise description of valid nesting: the order of elements, and their content.

All Doc Book documents must conform to this description or they are not Doc Book documents (by definition).

Message) Exit Sub End Select End Sub End Class In the above example, an Xml Schema Validation Exception will be thrown when Load is called if any attribute or element type does not match the corresponding type specified in the validating schema. Create Navigator() Dim validation As Validation Event Handler = New Validation Event Handler(Address Of Schema Validation Handler) navigator.

Load(reader) Dim navigator As XPath Navigator = document.

The first way to validate an XML document is to validate the document as it is loaded into an Xml Document object using a validating Xml Reader object. Create("contoso Books.xml", settings) Dim document As Xml Document = New Xml Document() document. The result is a previously untyped XML document in the Xml Document object replaced with a typed document. Load("contoso Books.xml") Dim navigator As XPath Navigator = document. Validate(validation) End Sub Shared Sub Schema Validation Handler(By Val sender As Object, By Val e As Validation Event Args) Select Case e.

The second way is to validate a previously untyped XML document using the Validate method of the Xml Document class. Load(reader) Dim navigator As XPath Navigator = document. The Xml Document object reports schema validation errors and warnings using the Validation Event Handler delegate passed as a parameter to the Validate method.

In both cases, changes to the validated XML document can be revalidated using the Validate method of the Xml Document class. The following example validates the Imports System Imports System.