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.
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
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.