BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XmlSerializer
XmlIncludeAttribute
XmlSerializer
Serialization Attributes
Serializable Classes
Deserializing an object
Serializing an object
Serialize Class
Metadata Attributes
<< XML DOM
XQuery >>

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :03/24/2008
Times viewed :2589

 

XmlElement Attribute Type

Attaching attributes to individual class members is another way to declare additional types. This can be useful when it is impossible to attach an XmlInclude attribute to a class to declare derived classes, but we can declare the derived classes on the field that is referencing the base class.

In the next example we create a new class VerySmallParkingLot with a field of the Vehicle base class from the previous section. We attach two XmlElement attributes to the field, one for each type we expect the member to reference at runtime. The class and its XML counterpart are shown above. Besides declaring the Car type for the field, the XmlElement attribute also causes the XmlSerializer to identify the object type through the element name instead of an xsi:type attribute.

public class VerySmallParkingLot

{

  public VerySmallParkingLot (){}

  [XmlElement(

    Type=typeof(Car))]

  [XmlElement(

    Type=typeof(Motorcycle))]

  public Vehicle ParkedVehicle;

}

<VerySmallParkingLot …>

  <Car>
   <Make>Ford</Make>

    <Model>Explorer</Model>

    <Year>1997</Year>

    <VIN>1234</VIN>

  </Car>

</VerySmallParkingLot>

The more prevalent use-case where we need to define types through multiple XmlElement attributes is when we develop a class mapping to an XML type containing which can contain different child elements. We express this flexible setup with the <choice> model group in an XSD schema. Generally, a model group defines usage rules for a group of elements, or in XML terminology: Particles. A model group defines which particles can occur in a group, in which order they occur in and how many times. The <choice> model group in particular defines a group of mutually exclusive particles. The next XML schema snippet shows a type description for an XML type roughly corresponding to the VerySmallParkingLot class from above. Each instance of the XML type VerySmallParkingLot, can either contain a ParkedMotorcycle element of type Motorcycle or a ParkedCar element of type Car. The difference to the class above is that the element names vary with the element type because the choice model group requires unique names for each particle.

Listing 9.5 An XML type definition with the choice model group

<xs:complexType name="VerySmallParkingLot">

  <xs:sequence>

    <xs:choice minOccurs="1" maxOccurs="1">

      <xs:element minOccurs="0" maxOccurs="1" name="ParkedMotorcycle"

        type="Motorcycle" />

      <xs:element minOccurs="0" maxOccurs="1" name="ParkedCar"

        type="Car" />

    </xs:choice>

  </xs:sequence>

</xs:complexType>

But specifying the element names for each particle is no big deal to XML serialization, as long as the each particle’s type is unique. You simply specify the element name for each type by setting the

ElementName property of the XmlElement attribute as shown below.

public class VerySmallParkingLot

{

  public VerySmallParkingLot (){}

  [XmlElement(
    ElementName="ParkedCar",
    Type=typeof(Car))]

  [XmlElement(

    ElementName="ParkedMotorcycle",

    Type=typeof(Motorcycle))]

  public Vehicle ParkedVehicle;

}

<VerySmallParkingLot …>

  <ParkedCar>
   <Make>Ford</Make>

    <Model>Explorer</Model>

    <Year>1997</Year>

    <VIN>1234</VIN>

  </ParkedCar>

</VerySmallParkingLot>


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Integration Specialist Needed - Wor
Virtualization Server Infrastructur
A great opportunity to Digital Vide
here is a greate opportunity as a S
A great opportunity as a Network En

View all Jobs (Add yours)
View all CV (Add yours)




swimming pool contractor
chicago web site design
saveonconferences uk rates
Web Hosting
bigfoot conferencing
Burberry sunglasses
answering service


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP