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

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 static void Validate(
	this XElement source,
	XmlSchemaObject partialValidationType,
	XmlSchemaSet schemas,
	ValidationEventHandler validationEventHandler
)

Parameters

source
Type: System.Xml.Linq.XElement

The XElement to validate.

partialValidationType
Type: System.Xml.Schema.XmlSchemaObject

An XmlSchemaObject that specifies the sub-tree to validate.

schemas
Type: System.Xml.Schema.XmlSchemaSet

An XmlSchemaSet to validate against.

validationEventHandler
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.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type XElement. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

ExceptionCondition
XmlSchemaValidationException

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.

string xsdMarkup =
    @"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
       <xsd:element name='Root'>
        <xsd:complexType>
         <xsd:sequence>
          <xsd:element name='Child1' minOccurs='1' maxOccurs='1'>
           <xsd:complexType>
            <xsd:sequence>
             <xsd:element name='GrandChild1' minOccurs='1' maxOccurs='1'/>
             <xsd:element name='GrandChild2' minOccurs='1' maxOccurs='2'/>
            </xsd:sequence>
           </xsd:complexType>
          </xsd:element>
         </xsd:sequence>
        </xsd:complexType>
       </xsd:element>
      </xsd:schema>";

XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));

XDocument doc1 = new XDocument(
    new XElement("Root",
        new XElement("Child1",
            new XElement("GrandChild1", "gc"),
            new XElement("GrandChild2", "gc")
        )
    )
);

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

Console.WriteLine();
Console.WriteLine("Validating Child1 after first edit ...");
XElement child1 = doc1.Element("Root").Element("Child1");
child1.Add(new XElement("GrandChild2", "gc"));
errors = false;
child1.Validate(child1.GetSchemaInfo().SchemaElement, schemas, (sender, e) =>
    {
        Console.WriteLine(e.Message);
        errors = true;
    });
Console.WriteLine("child1 {0}", errors ? "did not validate" : "validated");

// the following makes the Child1 element invalid according to the schema
child1.Add(new XElement("GrandChild3", "gc"));
Console.WriteLine();
Console.WriteLine("Validating Child1 after second edit ...");
child1.Validate(child1.GetSchemaInfo().SchemaElement, schemas, (sender, e) =>
    {
        Console.WriteLine(e.Message);
        errors = true;
    });
Console.WriteLine("child1 {0}", errors ? "did not validate" : "validated");

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

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft