Extensions.Validate Method (XElement, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler, Boolean)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Validates that an XElement sub-tree conforms to a specified XmlSchemaObject and an XmlSchemaSet, optionally populating the XML tree with the post-schema-validation infoset (PSVI).

Namespace:   System.Xml.Schema
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

public static void Validate(
	this XElement source,
	XmlSchemaObject partialValidationType,
	XmlSchemaSet schemas,
	ValidationEventHandler validationEventHandler,
	bool addSchemaInfo


Type: System.Xml.Linq.XElement

The XElement to validate.

Type: System.Xml.Schema.XmlSchemaObject

An XmlSchemaObject that specifies the sub-tree to validate.

Type: System.Xml.Schema.XmlSchemaSet

An XmlSchemaSet to validate against.

Type: System.Xml.Schema.ValidationEventHandler

A ValidationEventHandler for an event that occurs when the reader encounters validation errors. If null, throws an exception upon validation errors.

Type: System.Boolean

A Boolean indicating whether to populate the post-schema-validation infoset (PSVI).

Exception Condition

Thrown for XML Schema Definition Language (XSD) validation errors.

You can use this method to validate that a sub-tree (with an XElement at the root of the sub-tree) conforms to a schema. You typically use this method when you have modified a sub-tree, and you want to make sure that it still conforms to its schema. You could validate the entire document, but it takes less processing time to validate a just a sub-tree.

If addSchemaInfo is true, then this method populates the XML tree with the post-schema-validation infoset (PSVI).

There are two aspects of populating the XML tree with the PSVI.

First, an annotation is added to all nodes in the tree such that you can now call GetSchemaInfo on an element or attribute in the tree.

Second, default elements and attributes defined in the XSD are added to the XML tree. By calling one of the GetSchemaInfo methods, you can determine if a specific element or attribute was added from the XSD as a default element or attribute.

If you pass null for validationEventHandler, then this method raises an exception upon validation errors. Validation warnings will not raise an exception.

To validate a sub-tree, you use an instance of XmlSchemaObject. You can obtain this instance in various ways. An easy way is as follows:

  1. Validate that a document conforms to a schema.

  2. Add the post-schema-validation infoset (PSVI) by calling the Validate extension method.

  3. Call the GetSchemaInfo extension method to retrieve an object that implements IXmlSchemaInfo. From the retrieved object, you can get an XmlSchemaObject.

After you have an instance of an XmlSchemaObject, you can use this method to validate a sub-tree..

                string xsdMarkup =
    @"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
       <xsd:element name='Root'>
          <xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
          <xsd:element name='Child2' minOccurs='1' maxOccurs='1'>
             <xsd:extension base='xsd:string'>
              <xsd:attribute name='Att1' default='Att1 Default Value'/>
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));

XDocument doc1 = new XDocument(
    new XElement("Root",
        new XElement("Child1", "c1"),
        new XElement("Child2", "c2")

XDocument doc2 = new XDocument(
    new XElement("Root",
        new XElement("Child1", "content1"),
        new XElement("Child3", "content1")

Console.WriteLine("Validating doc1");
bool errors = false;
doc1.Validate(schemas, (o, e) =>
                         Console.WriteLine("{0}", e.Message);
                         errors = true;
                     }, true);
Console.WriteLine("doc1 {0}", errors ? "did not validate" : "validated");

Console.WriteLine("Validating doc2");
errors = false;
doc2.Validate(schemas, (o, e) =>
                         Console.WriteLine("{0}", e.Message);
                         errors = true;
                     }, true);
Console.WriteLine("doc2 {0}", errors ? "did not validate" : "validated");

Console.WriteLine("Contents of doc1:");

Console.WriteLine("Contents of doc2:");

This example produces the following output:

Validating doc1
doc1 validated

Validating doc2
The element 'Root' has invalid child element 'Child3'. List of possible elements expected: 'Child2'.
doc2 did not validate

Contents of doc1:
  <Child2 Att1="Att1 Default Value">c2</Child2>

Contents of doc2:

.NET Framework
Available since 3.5
Return to top