Sdílet prostřednictvím


Extensions.GetSchemaInfo Metoda

Definice

Získá po schema-validation infoset (PSVI) ověřeného uzlu.

Přetížení

Name Description
GetSchemaInfo(XAttribute)

Získá po schema-validation infoset (PSVI) ověřeného atributu.

GetSchemaInfo(XElement)

Získá post-schema-validation infoset (PSVI) ověřeného elementu.

Poznámky

Po ověření XDocumentmůžete načíst sadu informací po ověření schématu pro dokument XElement nebo XAttribute obsaženou v dokumentu.

Po načtení objektu IXmlSchemaInfo můžete pomocí SchemaAttributeSchemaElement vlastností získat částečný typ ověření (XmlSchemaElement nebo XmlSchemaAttribute). Částečné typy ověření můžete použít k ověření atributu nebo dílčího stromu.

GetSchemaInfo(XAttribute)

Zdroj:
XNodeValidator.cs
Zdroj:
XNodeValidator.cs
Zdroj:
XNodeValidator.cs
Zdroj:
XNodeValidator.cs
Zdroj:
XNodeValidator.cs

Získá po schema-validation infoset (PSVI) ověřeného atributu.

public:
[System::Runtime::CompilerServices::Extension]
 static System::Xml::Schema::IXmlSchemaInfo ^ GetSchemaInfo(System::Xml::Linq::XAttribute ^ source);
public static System.Xml.Schema.IXmlSchemaInfo? GetSchemaInfo(this System.Xml.Linq.XAttribute source);
public static System.Xml.Schema.IXmlSchemaInfo GetSchemaInfo(this System.Xml.Linq.XAttribute source);
static member GetSchemaInfo : System.Xml.Linq.XAttribute -> System.Xml.Schema.IXmlSchemaInfo
<Extension()>
Public Function GetSchemaInfo (source As XAttribute) As IXmlSchemaInfo

Parametry

source
XAttribute

Dříve XAttribute ověřená.

Návraty

A IXmlSchemaInfo obsahující sadu informací po ověření schématu pro objekt XAttribute.

Poznámky

Vrácenou metodou můžete použít IXmlSchemaInfo k určení určitých charakteristik ověřeného atributu. Můžete například určit, jestli atribut pochází z výchozí hodnoty atributu v XSD.

Tuto vlastnost použijete SchemaAttribute k získání částečného typu ověření (XmlSchemaAttribute). Můžete ho použít k opětovnému ověření atributu bez ověření celého dokumentu.

Příklad této vlastnosti naleznete v tématu Validate.

Platí pro

GetSchemaInfo(XElement)

Zdroj:
XNodeValidator.cs
Zdroj:
XNodeValidator.cs
Zdroj:
XNodeValidator.cs
Zdroj:
XNodeValidator.cs
Zdroj:
XNodeValidator.cs

Získá post-schema-validation infoset (PSVI) ověřeného elementu.

public:
[System::Runtime::CompilerServices::Extension]
 static System::Xml::Schema::IXmlSchemaInfo ^ GetSchemaInfo(System::Xml::Linq::XElement ^ source);
public static System.Xml.Schema.IXmlSchemaInfo? GetSchemaInfo(this System.Xml.Linq.XElement source);
public static System.Xml.Schema.IXmlSchemaInfo GetSchemaInfo(this System.Xml.Linq.XElement source);
static member GetSchemaInfo : System.Xml.Linq.XElement -> System.Xml.Schema.IXmlSchemaInfo
<Extension()>
Public Function GetSchemaInfo (source As XElement) As IXmlSchemaInfo

Parametry

source
XElement

Dříve XElement ověřená.

Návraty

A IXmlSchemaInfo , který obsahuje sadu informací po ověření schématu (PSVI) pro objekt XElement.

Příklady

Následující příklad naplní strom psVI. Po ověření vytiskne všechny prvky a atributy ve stromu, které jsou neplatné podle PSVI.

                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);
}
                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
    Next
    For Each child As XElement In el.Elements()
        DumpInvalidNodes(child)
    Next
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: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>

    Dim schemas As XmlSchemaSet = New XmlSchemaSet()
    schemas.Add("", xsdMarkup.CreateReader)

    Dim doc1 As XDocument = _
    <?xml version='1.0'?>
    <Root>
        <Child1>
            <GrandChild1>AAA</GrandChild1>
            <GrandChild2>ZZZ</GrandChild2>
            <GrandChild3>ZZZ</GrandChild3>
        </Child1>
    </Root>

    Console.WriteLine("Validating doc1 ...")
    errors = False
    doc1.Validate(schemas, AddressOf XSDErrors, True)
    Console.WriteLine("doc1 {0}", IIf(errors, "did not validate", "validated"))
    DumpInvalidNodes(doc1.Root)
End Sub

Tento příklad vytvoří následující výstup:

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

Poznámky

Vrácené touto metodou můžete použít IXmlSchemaInfo k určení určitých charakteristik ověřeného prvku. Můžete například určit typ dynamického schématu prvku.

Tuto vlastnost použijete SchemaElement k získání částečného typu ověření (XmlSchemaElement). Můžete ho použít k opětovnému ověření dílčího stromu s prvkem v jeho kořenovém adresáři bez ověření celého dokumentu.

Příklad této vlastnosti naleznete v tématu Validate.

Platí pro