映射XML订单
假定你在编写一个接受用户订单的应用程序,订单是XML格式的,它的XSD大纲如图1定义。该大纲定义了三种复合类型,分别提供订单的客户数据、订单数据和线性数据项。一个顶层Customer元素定义了XML文档的根。这个封闭的系统定义了元素之间的关系:Order元素包含了一个LineItem元素,Customer元素包含一个Order元素。图2显示了符合图1定义的大纲的一个XML文档实例。
图1:XSD大纲
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="urn:Sep2003Example" elementFormDefault="qualified"
xmlns="urn:Sep2003Example"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="OrderType">
<xs:sequence>
<xs:element name="OrderID" type="xs:integer" />
<xs:element name="LineItem" type="LineItemType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="LineItemType">
<xs:sequence>
<xs:element name="ProductID" type="xs:int" />
<xs:element name="Quantity" type="xs:int" />
<xs:element name="UnitPrice" type="xs:decimal" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="CustomerType">
<xs:sequence>
<xs:element name="CustomerID" type="xs:string" />
<xs:element name="Order" type="OrderType" />
</xs:sequence>
</xs:complexType>
<xs:element name="Customer" type="CustomerType">
</xs:element>
</xs:schema>
图2:一个XML文档示例
<?xml version="1.0" ?>
<Customer xmlns="urn:Sep2003Example">
<CustomerID>ALFKI</CustomerID>
<PO>9572658</PO>
<Address>
<Street>One Main Street</Street>
<City>Anywhere</City>
<State>NJ</State>
<Zip>08080</Zip> </Address>
<Order>
<OrderID>10966</OrderID>
<LineItem>
<ProductID>37</ProductID>
<UnitPrice>26.50</UnitPrice>
<Quantity>8</Quantity>
<Description>Gravad lax</Description>
</LineItem>
<LineItem>
<ProductID>56</ProductID>
<UnitPrice>38.00</UnitPrice>
<Quantity>12</Quantity>
<Description>Gnocchi di nonna Alice</Description>
</LineItem>
</Order>
</Customer>
列表1中显示的C#代码使用ReadXmlSchema方法把图1中的大纲载入一个叫作orderDS的数据集中。ReadXmlSchema建立了三个数据表,它们分别与大纲中定义的Customer、Order和LineItem元素对应。因此你可以验证这个大纲在关系数据缓存中建立了预期的表,printDSShape方法把每个表的名称写到控制台上,后面跟着列的列表和每列的数据类型。
评论加载中…
![]() |