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


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

This method validates that an XElement sub-tree conforms to a specified XmlSchemaObject and an XmlSchemaSet.

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

Public Shared Sub Validate (
	source As XElement,
	partialValidationType As XmlSchemaObject,
	schemas As XmlSchemaSet,
	validationEventHandler As ValidationEventHandler


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.

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 its root) 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 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 an sub-tree.

                Dim errors As Boolean = False

Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
    Console.WriteLine("{0}", e.Message)
    errors = True
End Sub

Sub Main()

    Dim xsdMarkup As XDocument = _
        <?xml version='1.0'?>
        <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='GrandChild1' minOccurs='1' maxOccurs='1'/>
                                    <xsd:element name='GrandChild2' minOccurs='1' maxOccurs='2'/>

    Dim schemas As XmlSchemaSet = New XmlSchemaSet()
    schemas.Add("", xsdMarkup.CreateReader)

    Dim doc1 As XDocument = _
        <?xml version='1.0'?>

    Console.WriteLine("Validating doc1 ...")
    errors = False
    doc1.Validate(schemas, AddressOf XSDErrors, True)
    Console.WriteLine("doc1 {0}", IIf(errors, "did not validate", "validated"))

    Console.WriteLine("Validating Child1 after first edit ...")
    Dim child1 As XElement = doc1.Element("Root").Element("Child1")
    errors = False
    child1.Validate(child1.GetSchemaInfo().SchemaElement, schemas, AddressOf XSDErrors)
    Console.WriteLine("child1 {0}", IIf(errors, "did not validate", "validated"))

    ' the following makes the Child1 element invalid according to the schema
    Console.WriteLine("Validating Child1 after second edit ...")
    child1.Validate(child1.GetSchemaInfo().SchemaElement, schemas, AddressOf XSDErrors)
    Console.WriteLine("child1 {0}", IIf(errors, "did not validate", "validated"))
End Sub

This example produces the following output:

Validating doc1 ...
doc1 validated

Validating Child1 after first edit ...
child1 validated

Validating Child1 after second edit ...
The element 'Child1' has invalid child element 'GrandChild3'.
child1 did not validate

.NET Framework
Available since 3.5
Return to top