Creating an XSD for XML serialized derived types

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Ive got a generic list of a base type (PetBase) that is getting serialized to XML via the XMLSerializer. Id like to create an xsd for this, and use it for intellisense when editing the xml document.

This class structure
<div style="color:Black; background-color:White
<pre> [Serializable]
<span style="color:Blue public <span style="color:Blue class PetData
{
<span style="color:Blue public List<PetBase> Pets { <span style="color:Blue get; <span style="color:Blue set; }
}
[Serializable]
<span style="color:Blue public <span style="color:Blue abstract <span style="color:Blue class PetBase
{
<span style="color:Blue public <span style="color:Blue string PetName { <span style="color:Blue get; <span style="color:Blue set; }
}

[Serializable]
<span style="color:Blue public <span style="color:Blue class Dog:PetBase
{
<span style="color:Blue public <span style="color:Blue string Breed { <span style="color:Blue get; <span style="color:Blue set; }
}

[Serializable]
<span style="color:Blue public <span style="color:Blue class Bird:PetBase
{
<span style="color:Blue public <span style="color:Blue string FeatherColor { <span style="color:Blue get; <span style="color:Blue set; }
<span style="color:Blue public <span style="color:Blue string BeakColor { <span style="color:Blue get; <span style="color:Blue set; }
}

[Serializable]
<span style="color:Blue public <span style="color:Blue class Fish:PetBase
{
<span style="color:Blue public <span style="color:Blue string ScaleColor { <span style="color:Blue get; <span style="color:Blue set; }
}
[/code]


is getting seralized to this xml data.


<div style="color:Black; background-color:White
<pre><span style="color:Blue <?<span style="color:#A31515 xml <span style="color:Red version<span style="color:Blue =<span style="color:Black "<span style="color:Blue 1.0<span style="color:Black " <span style="color:Red encoding<span style="color:Blue =<span style="color:Black "<span style="color:Blue utf-8<span style="color:Black "<span style="color:Blue ?>
<span style="color:Blue <<span style="color:#A31515 PetData <span style="color:Red xmlns:xsi<span style="color:Blue =<span style="color:Black "<span style="color:Blue http://www.w3.org/2001/XMLSchema-instance<span style="color:Black " <span style="color:Red xmlns:xsd<span style="color:Blue =<span style="color:Black "<span style="color:Blue http://www.w3.org/2001/XMLSchema<span style="color:Black "<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Pets<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PetBase <span style="color:Red xsi:type<span style="color:Blue =<span style="color:Black "<span style="color:Blue Dog<span style="color:Black "<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PetName<span style="color:Blue >Rover<span style="color:Blue </<span style="color:#A31515 PetName<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 Breed<span style="color:Blue >Poodle<span style="color:Blue </<span style="color:#A31515 Breed<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 PetBase<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PetBase <span style="color:Red xsi:type<span style="color:Blue =<span style="color:Black "<span style="color:Blue Bird<span style="color:Black "<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PetName<span style="color:Blue >Tweety<span style="color:Blue </<span style="color:#A31515 PetName<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 FeatherColor<span style="color:Blue >Yellow<span style="color:Blue </<span style="color:#A31515 FeatherColor<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 BeakColor<span style="color:Blue >Orange<span style="color:Blue </<span style="color:#A31515 BeakColor<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 PetBase<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PetBase <span style="color:Red xsi:type<span style="color:Blue =<span style="color:Black "<span style="color:Blue Fish<span style="color:Black "<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 PetName<span style="color:Blue >Charlie Tuna<span style="color:Blue </<span style="color:#A31515 PetName<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 ScaleColor<span style="color:Blue >Pink<span style="color:Blue </<span style="color:#A31515 ScaleColor<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 PetBase<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 Pets<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 PetData<span style="color:Blue >
[/code]


When I use Visual Studio to create a schema based on this document, I get the following generated for me.

<div style="color:Black; background-color:White
<pre><span style="color:Blue <?<span style="color:#A31515 xml <span style="color:Red version<span style="color:Blue =<span style="color:Black "<span style="color:Blue 1.0<span style="color:Black " <span style="color:Red encoding<span style="color:Blue =<span style="color:Black "<span style="color:Blue utf-8<span style="color:Black "<span style="color:Blue ?>
<span style="color:Blue <<span style="color:#A31515 xs<span style="color:Blue :<span style="color:#A31515 schema <span style="color:Red xmlns:xsi<span style="color:Blue =<span style="color:Black "<span style="color:Blue http://www.w3.org/2001/XMLSchema-instance<span style="color:Black " <span style="color:Red xmlns:xsd<span style="color:Blue =<span style="color:Black "<span style="color:Blue http://www.w3.org/2001/XMLSchema<span style="color:Black " <span style="color:Red xmlns:xs<span style="color:Blue =<span style="color:Black "<span style="color:Blue http://www.w3.org/2001/XMLSchema<span style="color:Black " <span style="color:Red attributeFormDefault<span style="color:Blue =<span style="color:Black "<span style="color:Blue unqualified<span style="color:Black " <span style="color:Red elementFormDefault<span style="color:Blue =<span style="color:Black "<span style="color:Blue qualified<span style="color:Black "<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 element <span style="color:Red name<span style="color:Blue =<span style="color:Black "<span style="color:Blue PetData<span style="color:Black "<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 complexType<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 sequence<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 element <span style="color:Red name<span style="color:Blue =<span style="color:Black "<span style="color:Blue Pets<span style="color:Black "<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 complexType<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 sequence<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 element <span style="color:Red maxOccurs<span style="color:Blue =<span style="color:Black "<span style="color:Blue unbounded<span style="color:Black " <span style="color:Red name<span style="color:Blue =<span style="color:Black "<span style="color:Blue PetBase<span style="color:Black "<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 complexType<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 sequence<span style="color:Blue >
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 element <span style="color:Red name<span style="color:Blue =<span style="color:Black "<span style="color:Blue PetName<span style="color:Black " <span style="color:Red type<span style="color:Blue =<span style="color:Black "<span style="color:Blue xsd:string<span style="color:Black " <span style="color:Blue />
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 element <span style="color:Red minOccurs<span style="color:Blue =<span style="color:Black "<span style="color:Blue 0<span style="color:Black " <span style="color:Red name<span style="color:Blue =<span style="color:Black "<span style="color:Blue ScaleColor<span style="color:Black " <span style="color:Red type<span style="color:Blue =<span style="color:Black "<span style="color:Blue xsd:string<span style="color:Black " <span style="color:Blue />
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 element <span style="color:Red minOccurs<span style="color:Blue =<span style="color:Black "<span style="color:Blue 0<span style="color:Black " <span style="color:Red name<span style="color:Blue =<span style="color:Black "<span style="color:Blue FeatherColor<span style="color:Black " <span style="color:Red type<span style="color:Blue =<span style="color:Black "<span style="color:Blue xsd:string<span style="color:Black " <span style="color:Blue />
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 element <span style="color:Red minOccurs<span style="color:Blue =<span style="color:Black "<span style="color:Blue 0<span style="color:Black " <span style="color:Red name<span style="color:Blue =<span style="color:Black "<span style="color:Blue BeakColor<span style="color:Black " <span style="color:Red type<span style="color:Blue =<span style="color:Black "<span style="color:Blue xsd:string<span style="color:Black " <span style="color:Blue />
<span style="color:Blue <<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 element <span style="color:Red minOccurs<span style="color:Blue =<span style="color:Black "<span style="color:Blue 0<span style="color:Black " <span style="color:Red name<span style="color:Blue =<span style="color:Black "<span style="color:Blue Breed<span style="color:Black " <span style="color:Red type<span style="color:Blue =<span style="color:Black "<span style="color:Blue xsd:string<span style="color:Black " <span style="color:Blue />
<span style="color:Blue </<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 sequence<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 complexType<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 element<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 sequence<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 complexType<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 element<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 sequence<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 complexType<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 xsd<span style="color:Blue :<span style="color:#A31515 element<span style="color:Blue >
<span style="color:Blue </<span style="color:#A31515 xs<span style="color:Blue :<span style="color:#A31515 schema<span style="color:Blue >
[/code]


The schema is not exactly what I wanted.

Id like the xsi:Type attribute of the PetBase element to be restricted to "Dog","Bird" and "Fish". When the xsi:Type is "Dog", only the PetsName and Breed attribute should be allowed. When the xsi:Type attribute value is "Bird", only PetsName,
FeatherColor, and BeakColor should be allowed, and so on.

How can I represent this in the XSD?

Thanks.

View the full article
 
Back
Top