This documentation is archived and is not being maintained.

Extensions.GetSchemaInfo Method (XElement)

Gets the post-schema-validation infoset (PSVI) of a validated element.

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

public static IXmlSchemaInfo GetSchemaInfo(
	this XElement source


Type: System.Xml.Linq.XElement
An XElement that has been previously validated.

Return Value

Type: System.Xml.Schema.IXmlSchemaInfo
A IXmlSchemaInfo that contains the post-schema-validation infoset (PSVI) for an XElement.

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

You can use the IXmlSchemaInfo returned by this method to determine certain characteristics of a validated element. For example, you can determine the dynamic schema type of the element.

You use the SchemaElement property to get a partial validation type (XmlSchemaElement). You can use it to revalidate a sub-tree with an element at its root without validating an entire document.

For an example of this property, see Validate.

The following example populates the tree with a PSVI. After validation, it prints all elements and attributes in the tree that are invalid according to the PSVI.

static void DumpInvalidNodes(XElement el)
    if (el.GetSchemaInfo().Validity != XmlSchemaValidity.Valid)
        Console.WriteLine("Invalid Element {0}",
            .Aggregate("", (s, i) => s + "/" + i.Name.ToString()));
    foreach (XAttribute att in el.Attributes())
        if (att.GetSchemaInfo().Validity != XmlSchemaValidity.Valid)
            Console.WriteLine("Invalid Attribute {0}",
                    (s, i) => s + "/" + i.Name.ToString()) + "/@" + att.Name.ToString()
    foreach (XElement child in el.Elements())

static void Main(string[] args)
    string xsdMarkup =
         @"<xsd:schema xmlns:xsd=''>
   <xsd:simpleType name='GCType'>
    <xsd:restriction base='xsd:token'>
     <xsd:enumeration value='AAA'/>
     <xsd:enumeration value='BBB'/>
   <xsd:element name='Root'>
      <xsd:element name='Child1' minOccurs='1' maxOccurs='1'>
         <xsd:element name='GrandChild1' type='GCType'/>
         <xsd:element name='GrandChild2' type='GCType'/>
         <xsd:element name='GrandChild3' type='GCType'/>

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

    XDocument doc1 = new XDocument(
        new XElement("Root",
            new XElement("Child1",
                new XElement("GrandChild1", "AAA"),
                new XElement("GrandChild2", "ZZZ"),
                new XElement("GrandChild3", "ZZZ")

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

This example produces the following output:

Validating doc1 ...
The 'GrandChild2' element is invalid - The value 'ZZZ' is invalid according to its datatype 'GCType' - The Enumeration constraint failed.
The 'GrandChild3' element is invalid - The value 'ZZZ' is invalid according to its datatype 'GCType' - The Enumeration constraint failed.
doc1 did not validate
Invalid Element /Root
Invalid Element /Root/Child1
Invalid Element /Root/Child1/GrandChild2
Invalid Element /Root/Child1/GrandChild3

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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