Extensions 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
此類別包含用於 XSD 驗證的 LINQ 轉 XML 擴充方法。
public ref class Extensions abstract sealed
public static class Extensions
type Extensions = class
Public Module Extensions
- 繼承
-
Extensions
範例
範例 XSD 檔案中的 XSD:客戶與訂單 包含一個可用來驗證範例 XML 檔案:客戶與訂單中 XML 文件的結構。 以下範例載入結構與文件,驗證文件,修改文件使 xs:key and xs: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)的方法。
當你 XDocument驗證 、 XElement或 XAttribute時,也可以選擇性地將 post-schema-validation 資訊集填充 XML 樹。 PSVI 資訊以 類型 System.Xml.Schema.XmlSchemaInfo的註解形式加入。