此代码仍然是演示如何使用LINQ to XML的XDocument.Validate()方法根据指定的XSD验证XDocument内容的有效性。
示例代码程序首先加载XSD和XML文档到内存,然后对其进行验证,第一次的验证成功地通过。紧接着,修改其中一个客户的编号,再对这个修改过的XML内容进行验证,则产生了异常。因为引用此客户编号的订单相关数据并没有被更新。如果使用没修改之前的客户编号来查找客户的话,自然是找不到了。
下面是用来验证XML的XSD架构定义:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name='Root'>
<xs:complexType>
<xs:sequence>
<xs:element name='Customers'>
<xs:complexType>
<xs:sequence>
<xs:element name='Customer' type='CustomerType' minOccurs='0' maxOccurs='unbounded' />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name='Orders'>
<xs:complexType>
<xs:sequence>
<xs:element name='Order' type='OrderType' minOccurs='0' maxOccurs='unbounded' />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name='CustomerIDKey'>
<xs:selector xpath='Customers/Customer'/>
<xs:field xpath='@CustomerID'/>
</xs:key>
<xs:keyref name='CustomerIDKeyRef' refer='CustomerIDKey'>
<xs:selector xpath='Orders/Order'/>
<xs:field xpath='CustomerID'/>
</xs:keyref>
</xs:element>
<xs:complexType name='CustomerType'>
<xs:sequence>
<xs:element name='CompanyName' type='xs:string'/>
<xs:element name='ContactName' type='xs:string'/>
<xs:element name='ContactTitle' type='xs:string'/>
<xs:element name='Phone' type='xs:string'/>
<xs:element name='Fax' minOccurs='0' type='xs:string'/>
<xs:element name='FullAddress' type='AddressType'/>
</xs:sequence>
<xs:attribute name='CustomerID' type='xs:token'/>
</xs:complexType>
<xs:complexType name='AddressType'>
<xs:sequence>
<xs:element name='Address' type='xs:string'/>
<xs:element name='City' type='xs:string'/>
<xs:element name='Region' type='xs:string'/>
<xs:element name='PostalCode' type='xs:string' />
<xs:element name='Country' type='xs:string'/>
</xs:sequence>
<xs:attribute name='CustomerID' type='xs:token'/>
</xs:complexType>
<xs:complexType name='OrderType'>
<xs:sequence>
<xs:element name='CustomerID' type='xs:token'/>
<xs:element name='EmployeeID' type='xs:token'/>
<xs:element name='OrderDate' type='xs:dateTime'/>
<xs:element name='RequiredDate' type='xs:dateTime'/>
<xs:element name='ShipInfo' type='ShipInfoType'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='ShipInfoType'>
<xs:sequence>
<xs:element name='ShipVia' type='xs:integer'/>
<xs:element name='Freight' type='xs:decimal'/>
<xs:element name='ShipName' type='xs:string'/>
<xs:element name='ShipAddress' type='xs:string'/>
<xs:element name='ShipCity' type='xs:string'/>
<xs:element name='ShipRegion' type='xs:string'/>
<xs:element name='ShipPostalCode' type='xs:string'/>
<xs:element name='ShipCountry' type='xs:string'/>
</xs:sequence>
<xs:attribute name='ShippedDate' type='xs:dateTime'/>
</xs:complexType>
</xs:schema>
下面这是要进行验证的XML文档内容:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name='Root'>
<xs:complexType>
<xs:sequence>
<xs:element name='Customers'>
<xs:complexType>
<xs:sequence>
<xs:element name='Customer' type='CustomerType' minOccurs='0' maxOccurs='unbounded' />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name='Orders'>
<xs:complexType>
<xs:sequence>
<xs:element name='Order' type='OrderType' minOccurs='0' maxOccurs='unbounded' />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name='CustomerIDKey'>
<xs:selector xpath='Customers/Customer'/>
<xs:field xpath='@CustomerID'/>
</xs:key>
<xs:keyref name='CustomerIDKeyRef' refer='CustomerIDKey'>
<xs:selector xpath='Orders/Order'/>
<xs:field xpath='CustomerID'/>
</xs:keyref>
</xs:element>
<xs:complexType name='CustomerType'>
<xs:sequence>
<xs:element name='CompanyName' type='xs:string'/>
<xs:element name='ContactName' type='xs:string'/>
<xs:element name='ContactTitle' type='xs:string'/>
<xs:element name='Phone' type='xs:string'/>
<xs:element name='Fax' minOccurs='0' type='xs:string'/>
<xs:element name='FullAddress' type='AddressType'/>
</xs:sequence>
<xs:attribute name='CustomerID' type='xs:token'/>
</xs:complexType>
<xs:complexType name='AddressType'>
<xs:sequence>
<xs:element name='Address' type='xs:string'/>
<xs:element name='City' type='xs:string'/>
<xs:element name='Region' type='xs:string'/>
<xs:element name='PostalCode' type='xs:string' />
<xs:element name='Country' type='xs:string'/>
</xs:sequence>
<xs:attribute name='CustomerID' type='xs:token'/>
</xs:complexType>
<xs:complexType name='OrderType'>
<xs:sequence>
<xs:element name='CustomerID' type='xs:token'/>
<xs:element name='EmployeeID' type='xs:token'/>
<xs:element name='OrderDate' type='xs:dateTime'/>
<xs:element name='RequiredDate' type='xs:dateTime'/>
<xs:element name='ShipInfo' type='ShipInfoType'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='ShipInfoType'>
<xs:sequence>
<xs:element name='ShipVia' type='xs:integer'/>
<xs:element name='Freight' type='xs:decimal'/>
<xs:element name='ShipName' type='xs:string'/>
<xs:element name='ShipAddress' type='xs:string'/>
<xs:element name='ShipCity' type='xs:string'/>
<xs:element name='ShipRegion' type='xs:string'/>
<xs:element name='ShipPostalCode' type='xs:string'/>
<xs:element name='ShipCountry' type='xs:string'/>
</xs:sequence>
<xs:attribute name='ShippedDate' type='xs:dateTime'/>
</xs:complexType>
</xs:schema>
示例代码如下:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml.Linq;
namespace Demo06Ex02
{
class Program
{
static void Main(string[] args)
{
// *************************************************
// 这里对没有修改过的XML文档进行验证,能够通过。
// *************************************************
XmlSchemaSet Schemas = new XmlSchemaSet();
Schemas.Add("", @"C:/LINQ/CustomersOrders.xsd");
Console.WriteLine("Attempting to validate...");
XDocument Document = XDocument.Load(@"C:/LINQ/CustomersOrders.xml");
bool Errors = false;
Document.Validate(Schemas, (o, e) =>
{
Console.WriteLine(e.Message);
Errors = true;
});
Console.WriteLine("CustomersOrders.xml {0}", Errors ?
"did not validate" : "validated");
Console.WriteLine();
// ********************************************************
// 修改了客户的编号之后,再次验证就引发了异常。
// ********************************************************
Document.Root.Element("Orders").Element("Order")
.Element("CustomerID").Value = "AAAAA";
Console.WriteLine("Attempting to validate after modification...");
Errors = false;
Document.Validate(Schemas, (o, e) =>
{
Console.WriteLine(e.Message);
Errors = true;
});
Console.WriteLine("CustomersOrders.xml {0}", Errors ?
"did not validate" : "validated");
}
}
}