Extensions.GetSchemaInfo Method

Definition

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

Overloads

GetSchemaInfo(XAttribute)

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

GetSchemaInfo(XElement)

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

Remarks

After you have validated an XDocument, you can retrieve the post-schema-validation infoset for an XElement or XAttribute that is contained in the document.

After retrieving the IXmlSchemaInfo object, you can use the SchemaAttribute or SchemaElement properties to get a partial validation type (XmlSchemaElement or XmlSchemaAttribute). You can use the partial validation types to validate an attribute or a sub-tree.

GetSchemaInfo(XAttribute)

Source:
XNodeValidator.cs
Source:
XNodeValidator.cs
Source:
XNodeValidator.cs

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

C#
public static System.Xml.Schema.IXmlSchemaInfo? GetSchemaInfo(this System.Xml.Linq.XAttribute source);
C#
public static System.Xml.Schema.IXmlSchemaInfo GetSchemaInfo(this System.Xml.Linq.XAttribute source);

Parameters

source
XAttribute

An XAttribute that has been previously validated.

Returns

A IXmlSchemaInfo that contains the post-schema-validation infoset for an XAttribute.

Remarks

You can use the IXmlSchemaInfo returned by this method to determine certain characteristics of a validated attribute. For example, you can determine if the attribute came from a default attribute value in an XSD.

You use the SchemaAttribute property to get a partial validation type (XmlSchemaAttribute). You can use it to revalidate an attribute without validating an entire document.

For an example of this property, see Validate.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetSchemaInfo(XElement)

Source:
XNodeValidator.cs
Source:
XNodeValidator.cs
Source:
XNodeValidator.cs

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

C#
public static System.Xml.Schema.IXmlSchemaInfo? GetSchemaInfo(this System.Xml.Linq.XElement source);
C#
public static System.Xml.Schema.IXmlSchemaInfo GetSchemaInfo(this System.Xml.Linq.XElement source);

Parameters

source
XElement

An XElement that has been previously validated.

Returns

A IXmlSchemaInfo that contains the post-schema-validation infoset (PSVI) for an XElement.

Examples

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.

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

static void Main(string[] args)  
{  
    string xsdMarkup =  
         @"<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:restriction>  
   </xsd:simpleType>  
   <xsd:element name='Root'>  
    <xsd:complexType>  
     <xsd:sequence>  
      <xsd:element name='Child1' minOccurs='1' maxOccurs='1'>  
       <xsd:complexType>  
        <xsd:sequence>  
         <xsd:element name='GrandChild1' type='GCType'/>  
         <xsd:element name='GrandChild2' type='GCType'/>  
         <xsd:element name='GrandChild3' type='GCType'/>  
        </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", "AAA"),  
                new XElement("GrandChild2", "ZZZ"),  
                new XElement("GrandChild3", "ZZZ")  
            )  
        )  
    );  

    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");  
    DumpInvalidNodes(doc1.Root);  
}  

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  

Remarks

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.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1