Creating Derived Datatypes

Creating an Open PurchaseOrder Schema

Sometimes it is necessary to allow data to be included within an instance of a particular XML datatype even if you did not anticipate this when you created the schema. For example, a company might submit a purchase order containing account information to be used to pay for shipping the requested items. Schemas that allow the inclusion of additional elements and attributes that are not formally defined within the schema itself are often referred to as open schemas.

You can create an open schema by decorating a public field or property with the XmlAnyAttribute and XmlAnyElement attributes. The XmlAnyAttribute attribute specifies that the parent element can contain any XML attribute in addition to the ones formally defined within the schema. The XmlAnyElement attribute specifies that the parent element can contain any XML element in addition to the ones formally defined within the schema.

The flexibility of open schemas can be very enticing. However, you should consider the consequences before you create an open schema because the code needed to handle and process the extended data can become very complex.

One popular method for allowing extended information to be included within an instance document in a semi-controlled fashion is to provide an area in the document definition for that information. The following example defines an element within the PurchaseOrder datatype that is designated for containing extended information about the purchase order:

[XmlRoot("PurchaseOrder", IsNullable=false)] [XmlInclude(typeof(CommentedPurchaseOrder))] public class PurchaseOrder {     [XmlElement(IsNullable=false)]     public Address BillingAddress;     [XmlElement(IsNullable=false)]     public Address ShippingAddress;     [XmlArray("Items", IsNullable=false)]     [XmlArrayItem("Item", IsNullable=false)]     public PurchaseOrderItem [] Items;     [XmlElement("AdditionalInfo")]     public AdditionalInfo Info;     // Additional type definitions... } // Previously defined type definitions removed for clarity... public class AdditionalInfo {     [XmlAnyAttribute]     public XmlElement AdditionalAttributes;     [XmlAnyElement]     public XmlElement [] AdditionalElements; }

The preceding code defines an additional element within the PurchaseOrder XML datatype called AdditionalInfo. This element can contain any attribute as well as any element. The above code generates the following schema containing the AdditionalInfo element definition:

<schema attributeFormDefault="qualified" elementFormDefault="qualified"  targetNamespace="http://tempuri.org/">   <element name="PurchaseOrder" type="s0:PurchaseOrder" />   <complexType name="PurchaseOrder">     <sequence>       <element minOccurs="1" maxOccurs="1" name="BillingAddress"         type="s0:Address" />       <element minOccurs="1" maxOccurs="1" name="ShippingAddress"        type="s0:Address" />       <element minOccurs="1" maxOccurs="1" name="Items"        type="s0:ArrayOfPurchaseOrderItem" />       <element minOccurs="1" maxOccurs="1" name="AdditionalInfo"        type="s0:AdditionalInfo" />    </sequence>   </complexType>   <!-- Additional definitions removed for clarity -->   <complexType name="AdditionalInfo">     <sequence>       <any minOccurs="0" maxOccurs="unbounded" />     <sequence>     <anyAttribute />   </complexType></schema>

The public field or property decorated by the XmlAnyAttribute attribute can be of type XmlElement or XmlNode. Because the XmlElement and XmlNode types are part of the XML DOM that ships with .NET, you can use the methods and properties exposed by these types to navigate through the additional data that accompanied the purchase order.



Building XML Web Services for the Microsoft  .NET Platform
Building XML Web Services for the Microsoft .NET Platform
ISBN: 0735614067
EAN: 2147483647
Year: 2002
Pages: 94
Authors: Scott Short

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net