.

Validating XML using an XSD Schema


How to Validate XML by Using an XSD Schema

To validate an XML document against an XSD schema, take the following steps:


1. Create an XmlValidatingReader object as a wrapper around an XmlTextReader
object, as shown in the following code:
' Create an XmlValidatingReader object, to read
and validate Product.xml
XmlTextReader tr = new XmlTextReader("Product.xml");
XmlValidatingReader vr = new XmlValidatingReader(tr);


2. Specify the type of validation required, by using the ValidationType
enumeration.
The .NET Framework supports three types of XML validation:
  – Document type definitions (DTDs); specify ValidationType.DTD.
  – Microsoft XML data-reduced (XDR) schemas; specify ValidationType.XDR.
  – W3C-standard XSD schemas; specify ValidationType.Schema.

The following code shows the use of the ValidationType enumeration:
vr.ValidationType = ValidationType.Schema; '
Specify XSD schema validation


3. Register a validation event-handler method, as shown in the following
code:
vr.ValidationEventHandler += new
ValidationEventHandler(MyHandlerMethod);


4. Provide an implementation for the validation event-handler method, as shown
in
the following code:

What's your thoughts on this?

*

Protected by WP Anti Spam