Defining the Items Array

Creating Derived Datatypes

XML serialization supports defining XML datatypes that are derived by extension from other XML datatypes. You can define a derived XML datatype by creating a derived .NET type and then decorating the base type with the XmlInclude attribute.

The following example defines a base type called Tire and creates two derived types called AutoTire and MountainBikeTire. The XmlInclude attribute is also used to notify XML serialization of these two derived types.

[XmlInclude(typeof(AutoTire))] [XmlInclude(typeof(MountainBikeTire))] public class Tire {     public int WheelDiameter;     public int Width; } public class AutoTire : Tire {     public AspectRatio; } public class MountainBikeTire : Tire {     public TireLocationType Position; } public enum TireLocationType {     FRONT,     REAR }

The preceding code will generate an XML datatype called Tire and two datatypes that derive by extension from Tire: AutoTire and MountainBikeTire. Because AutoTire and MountainBikeTire are extended versions of the Tire datatype, Web service interfaces that accept the Tire datatype can also accept instances of AutoTire and MountainBikeTire.

Recall that I want to specify that the Comments element within the PurchaseOrder document be optional. Also recall that you cannot specify optional parameters for a Web service by explicitly setting the minOccurs restriction on the element declaration to 0. One potential workaround uses derived types and the XmlInclude attribute.

The following code creates a type that is derived from PurchaseOrder. The new type contains the optional Comments element.

[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;     // Additional type definitions... } public class CommentedPurchaseOrder : PurchaseOrder {     [XmlElement(IsNullable=true, DataType="normalizedString")]     public string Comments; }

The preceding example defines the CommentedPurchaseOrder XML datatype. It extends the PurchaseOrder datatype by allowing a Comments element to be included within a 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