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 Shared Function GetSchemaInfo (
	source As XElement
) As IXmlSchemaInfo


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.

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.

                Private Sub DumpInvalidNodes(ByVal el As XElement)
    If el.GetSchemaInfo.Validity <> XmlSchemaValidity.Valid Then
        Console.WriteLine("Invalid Element {0}", _
            el _
            .AncestorsAndSelf _
            .InDocumentOrder() _
            .Aggregate("", _
                Function(ByVal s, ByVal i) s + "/" + i.Name.ToString()))
    End If
    For Each att As XAttribute In el.Attributes()
        If att.GetSchemaInfo.Validity <> XmlSchemaValidity.Valid Then
            Console.WriteLine("Invalid Attribute {0}", _
                att _
                .Parent _
                .AncestorsAndSelf() _
                .InDocumentOrder() _
                .Aggregate("", _
                    Function(ByVal s, ByVal i) s + "/" + i.Name.ToString()) + _
                    "/@" + att.Name.ToString())
        End If
    For Each child As XElement In el.Elements()
End Sub

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: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'/>

    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"))
End Sub

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
Available since 3.5
Return to top