Extensions 類別

定義

這個類別包含 XSD 驗證的 LINQ to XML 延伸模組。

public ref class Extensions abstract sealed
public static class Extensions
type Extensions = class
Public Module Extensions
繼承
Extensions

範例

範例 XSD 檔案中的 XSD:客戶和訂單包含可用來驗證範例 XML 檔案中 XML 檔的架構:客戶和訂單。 下列範例會載入架構和檔、驗證檔、變更檔,使 xs:keyxs:keyref 關聯性無效,然後嘗試再次驗證。

                XmlSchemaSet schemas = new XmlSchemaSet();  
schemas.Add("", "CustomersOrders.xsd");  

XDocument custOrd = XDocument.Load("CustomersOrders.xml");  

Console.WriteLine("Validating custOrd");  
bool errors = false;  
custOrd.Validate(schemas, (o, e) =>  
                     {  
                         Console.WriteLine("{0}", e.Message);  
                         errors = true;  
                     });  
Console.WriteLine("custOrd {0}", errors ? "did not validate" : "validated");  

// Modify the custOrd tree so that it is no longer valid.  
custOrd.Root.Element("Orders").Element("Order").Element("CustomerID").Value = "AAAAA";  

Console.WriteLine();  
Console.WriteLine("Validating custOrd");  
errors = false;  
custOrd.Validate(schemas, (o, e) =>  
                     {  
                         Console.WriteLine("{0}", e.Message);  
                         errors = true;  
                     });  
Console.WriteLine("custOrd {0}", errors ? "did not validate" : "validated");  
                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 schemas As XmlSchemaSet = New XmlSchemaSet()  
    schemas.Add("", "CustomersOrders.xsd")  

    Console.WriteLine("Validating custOrd")  
    Dim custOrd As XDocument = XDocument.Load("CustomersOrders.xml")  
    errors = False  
    custOrd.Validate(schemas, AddressOf XSDErrors)  
    Console.WriteLine("custOrd {0}", IIf(errors, "did not validate", "validated"))  

    Console.WriteLine()  
    Console.WriteLine("Validating custOrd")  
    ' Modify the source document so that it will not validate.  
    custOrd.Root.Element("Orders").Element("Order").Element("CustomerID").Value = "AAAAA"  
    errors = False  
    custOrd.Validate(schemas, AddressOf XSDErrors)  
    Console.WriteLine("custOrd {0}", IIf(errors, "did not validate", "validated"))  
End Sub  

這個範例會產生下列輸出:

Validating custOrd  
custOrd validated  

Validating custOrd  
The key sequence 'AAAAA' in Keyref fails to refer to some key.  
custOrd did not validate  

備註

這個類別也包含取得已驗證 XML 節點之 PSVI) (架構後驗證資訊集的方法。

當您驗證 XDocumentXElementXAttribute 時,您也可以選擇性地使用架構後驗證資訊集填入 XML 樹狀結構。 PSVI 資訊會新增為 類型的 System.Xml.Schema.XmlSchemaInfo 注釋。

方法

GetSchemaInfo(XAttribute)

取得已驗證屬性的後結構描述驗證資訊集 (PSVI)。

GetSchemaInfo(XElement)

取得已驗證項目的後結構描述驗證資訊集 (PSVI)。

Validate(XAttribute, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler)

這個方法會驗證 XAttribute 是否符合指定的 XmlSchemaObjectXmlSchemaSet

Validate(XAttribute, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler, Boolean)

驗證 XAttribute 是否符合指定的 XmlSchemaObjectXmlSchemaSet,並選擇性地將後結構描述驗證資訊集 (PSVI) 填入 XML 樹狀目錄。

Validate(XDocument, XmlSchemaSet, ValidationEventHandler)

這個方法會驗證 XDocument 是否符合 XmlSchemaSet 中的 XSD。

Validate(XDocument, XmlSchemaSet, ValidationEventHandler, Boolean)

驗證 XDocument 是否符合 XmlSchemaSet 中的 XSD,並選擇性地將後結構描述驗證資訊集 (PSVI) 填入 XML 樹狀目錄。

Validate(XElement, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler)

這個方法會驗證 XElement 樹狀子目錄是否符合指定的 XmlSchemaObjectXmlSchemaSet

Validate(XElement, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler, Boolean)

驗證 XElement 樹狀子目錄是否符合指定的 XmlSchemaObjectXmlSchemaSet,並選擇性地將後結構描述驗證資訊集 (PSVI) 填入 XML 樹狀目錄。

適用於