This documentation is archived and is not being maintained.

Extensions.Validate Method (XDocument, XmlSchemaSet, ValidationEventHandler)

This method validates that an XDocument conforms to an XSD in an XmlSchemaSet.

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

public static void Validate(
	this XDocument source,
	XmlSchemaSet schemas,
	ValidationEventHandler validationEventHandler


Type: System.Xml.Linq.XDocument

The XDocument 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.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type XDocument. 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).


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

This extension method validates that the XDocument conforms to the schema content model in XmlSchemaSet.

The following example creates an XmlSchemaSet, then validates two XDocument objects against the schema set. One of the documents is valid, the other is not.

string xsdMarkup =
    @"<xsd:schema xmlns:xsd=''>
       <xsd:element name='Root'>
          <xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
          <xsd:element name='Child2' minOccurs='1' maxOccurs='1'/>
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));

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

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;
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;
Console.WriteLine("doc2 {0}", errors ? "did not validate" : "validated");

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

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5