Extensions.GetSchemaInfo Método

Definição

Obtém o PSVI (infoset de validação pós-esquema) de um nó validado.Gets the post-schema-validation infoset (PSVI) of a validated node.

Sobrecargas

GetSchemaInfo(XAttribute)

Obtém o PSVI (infoset de validação pós-esquema) de um atributo validado.Gets the post-schema-validation infoset (PSVI) of a validated attribute.

GetSchemaInfo(XElement)

Obtém o PSVI (post-schema-validation infoset) de um elemento validado.Gets the post-schema-validation infoset (PSVI) of a validated element.

Comentários

Depois de validar um XDocument , você pode recuperar o infoset de validação pós-esquema para um XElement ou XAttribute que está contido no documento.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.

Depois de recuperar o IXmlSchemaInfo objeto, você pode usar as SchemaAttribute SchemaElement Propriedades ou para obter um tipo de validação parcial ( XmlSchemaElement ou XmlSchemaAttribute ).After retrieving the IXmlSchemaInfo object, you can use the SchemaAttribute or SchemaElement properties to get a partial validation type (XmlSchemaElement or XmlSchemaAttribute). Você pode usar os tipos de validação parciais para validar um atributo ou uma subárvore.You can use the partial validation types to validate an attribute or a sub-tree.

GetSchemaInfo(XAttribute)

Obtém o PSVI (infoset de validação pós-esquema) de um atributo validado.Gets the post-schema-validation infoset (PSVI) of a validated attribute.

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

Parâmetros

source
XAttribute

Um XAttribute que foi validado anteriormente.An XAttribute that has been previously validated.

Retornos

IXmlSchemaInfo

Um IXmlSchemaInfo que contém o infoset de validação pós-esquema para um XAttribute.A IXmlSchemaInfo that contains the post-schema-validation infoset for an XAttribute.

Comentários

Você pode usar o IXmlSchemaInfo retornado por esse método para determinar determinadas características de um atributo validado.You can use the IXmlSchemaInfo returned by this method to determine certain characteristics of a validated attribute. Por exemplo, você pode determinar se o atributo veio de um valor de atributo padrão em um XSD.For example, you can determine if the attribute came from a default attribute value in an XSD.

Você usa a SchemaAttribute propriedade para obter um tipo de validação parcial ( XmlSchemaAttribute ).You use the SchemaAttribute property to get a partial validation type (XmlSchemaAttribute). Você pode usá-lo para revalidar um atributo sem validar um documento inteiro.You can use it to revalidate an attribute without validating an entire document.

Para obter um exemplo dessa propriedade, consulte Validate .For an example of this property, see Validate.

Aplica-se a

GetSchemaInfo(XElement)

Obtém o PSVI (post-schema-validation infoset) de um elemento validado.Gets the post-schema-validation infoset (PSVI) of a validated element.

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

Parâmetros

source
XElement

Um XElement que foi validado anteriormente.An XElement that has been previously validated.

Retornos

IXmlSchemaInfo

Uma IXmlSchemaInfo que contém o PSVI (post-schema-validation infoset) para um XElement.A IXmlSchemaInfo that contains the post-schema-validation infoset (PSVI) for an XElement.

Exemplos

O exemplo a seguir popula a árvore com um PSVI.The following example populates the tree with a PSVI. Após a validação, ele imprime todos os elementos e atributos na árvore que são inválidos de acordo com o 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}",  
            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  

Esse exemplo gera a saída a seguir: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  

Comentários

Você pode usar o IXmlSchemaInfo retornado por esse método para determinar determinadas características de um elemento validado.You can use the IXmlSchemaInfo returned by this method to determine certain characteristics of a validated element. Por exemplo, você pode determinar o tipo de esquema dinâmico do elemento.For example, you can determine the dynamic schema type of the element.

Você usa a SchemaElement propriedade para obter um tipo de validação parcial ( XmlSchemaElement ).You use the SchemaElement property to get a partial validation type (XmlSchemaElement). Você pode usá-lo para revalidar uma subárvore com um elemento em sua raiz sem validar um documento inteiro.You can use it to revalidate a sub-tree with an element at its root without validating an entire document.

Para obter um exemplo dessa propriedade, consulte Validate .For an example of this property, see Validate.

Aplica-se a