Extensions.GetSchemaInfo 메서드

정의

유효성이 검사된 노드의 PSVI(스키마 유효성 검사 이후 정보 집합)를 가져옵니다.

오버로드

GetSchemaInfo(XAttribute)

유효성이 검사된 특성의 PSVI(스키마 유효성 검사 이후 정보 집합)를 가져옵니다.

GetSchemaInfo(XElement)

유효성이 검사된 요소의 PSVI(스키마 유효성 검사 이후 정보 집합)를 가져옵니다.

설명

의 유효성을 검사한 XDocument후 문서에 포함된 또는 XAttribute 에 대한 XElement 스키마 유효성 검사 후 정보 세트를 검색할 수 있습니다.

개체를 검색한 IXmlSchemaInfo 후 또는 SchemaElement 속성을 사용하여 SchemaAttribute 부분 유효성 검사 형식(XmlSchemaElement 또는 XmlSchemaAttribute)을 가져올 수 있습니다. 부분 유효성 검사 형식을 사용하여 특성 또는 하위 트리의 유효성을 검사할 수 있습니다.

GetSchemaInfo(XAttribute)

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

유효성이 검사된 특성의 PSVI(스키마 유효성 검사 이후 정보 집합)를 가져옵니다.

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

매개 변수

source
XAttribute

이전에 유효성을 검사한 XAttribute입니다.

반환

IXmlSchemaInfo에 대한 스키마 유효성 검사 이후 정보 집합이 포함된 XAttribute입니다.

설명

이 메서드에서 반환된 를 IXmlSchemaInfo 사용하여 유효성이 검사된 특성의 특정 특성을 확인할 수 있습니다. 예를 들어 특성이 XSD의 기본 특성 값에서 왔는지 확인할 수 있습니다.

사용 하 여는 SchemaAttribute 부분 유효성 검사 형식 (XmlSchemaAttribute)을 가져올 속성입니다. 전체 문서의 유효성을 검사하지 않고 특성의 유효성을 다시 검사하는 데 사용할 수 있습니다.

이 속성의 예제는 를 참조하세요 Validate.

적용 대상

GetSchemaInfo(XElement)

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

유효성이 검사된 요소의 PSVI(스키마 유효성 검사 이후 정보 집합)를 가져옵니다.

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

매개 변수

source
XElement

이전에 유효성을 검사한 XElement입니다.

반환

IXmlSchemaInfo에 대한 PSVI(스키마 유효성 검사 이후 정보 집합)가 포함된 XElement입니다.

예제

다음 예제에서는 트리를 PSVI로 채웁니다. 유효성 검사 후 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  

이 예제는 다음과 같은 출력을 생성합니다.

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  

설명

이 메서드에서 반환된 를 IXmlSchemaInfo 사용하여 유효성이 검사된 요소의 특정 특성을 확인할 수 있습니다. 예를 들어 요소의 동적 스키마 유형을 확인할 수 있습니다.

사용 하 여는 SchemaElement 부분 유효성 검사 형식 (XmlSchemaElement)을 가져올 속성입니다. 전체 문서의 유효성을 검사하지 않고 루트에 요소가 있는 하위 트리의 유효성을 다시 검사하는 데 사용할 수 있습니다.

이 속성의 예제는 를 참조하세요 Validate.

적용 대상