XSD.EXE Genâd C# Deserializing w/ .xsd Subst Group
Hi,
I am trying to figure out how to get XSD.EXE (when generating C# classes from an .xsd) to add the XmlArrayItemAttribute Deserization Annotations to my C# generated class output: My specific case is when the .xsd has an element that holds an array of substitutionGroup'ed items.
Following is an example of a matching set of xsd, xml, xsd.exe command to generate C#, and the output C#.
When running a unit-test on the C#, the serialization only populates correctly if I manually add the following XmlArrayItemAttributes to the generated C# (directly below, and in complete context in the code block later in this post).
Can anyone make a suggestion of how to make XSD.EXE add these annotations (or another method of having this deserialize correctly)?
Thanks!
******MANUALLY ADDED XmlArrayItemAttribute ANNOTATIONS****** (see below Code Block for complete context)
[System.Xml.Serialization.XmlArrayItemAttribute("El ephant", typeof(ElephantType))]
[System.Xml.Serialization.XmlArrayItemAttribute("Ti ger", typeof(TigerType))]
[System.Xml.Serialization.XmlArrayItemAttribute("Mo nkey", typeof(MonkeyType))]
public AnimalAbstractType[] Animals {
get {
return this.animalsField;
}
...
*******ANIMAL.XSD********
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Zoo" type="ZooType"/>
<xs:complexType name="ZooType">
<xs:sequence>
<xs:element name="Animals" type="AnimalsType" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="AnimalsType">
<xs:sequence>
<xs:element ref="Animal" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:element name="Animal" type="AnimalAbstractType" abstract="true"/>
<xs:element name="Monkey" substitutionGroup="Animal" type="MonkeyType"/>
<xs:element name="Tiger" substitutionGroup="Animal" type="TigerType"/>
<xs:element name="Elephant" substitutionGroup="Animal" type="ElephantType"/>
<xs:complexType name="AnimalAbstractType">
<xs:sequence>
<xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="MonkeyType">
<xs:complexContent>
<xs:extension base="AnimalAbstractType">
<xs:sequence>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="TigerType">
<xs:complexContent>
<xs:extension base="AnimalAbstractType">
<xs:sequence>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="ElephantType">
<xs:complexContent>
<xs:extension base="AnimalAbstractType">
<xs:sequence>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
*******ANIMAL.XML********
<Zoo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Source\Pear\Prot otype\ImportExport\ImportExport\Animal.xsd">
<Animals>
<Monkey>
<Name>Curious George</Name>
</Monkey>
<Tiger>
<Name>Tigger</Name>
</Tiger>
<Elephant>
<Name>Dumbo</Name>
</Elephant>
</Animals>
</Zoo>
*******ANIMAL.BAT********
xsd /classes /language:cs Animal.xsd /namespace:Rayrew.Pear.ImportExport.Xml
*******ANIMAL.CS********
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:2.0.50727.832
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//
namespace Rayrew.Pear.ImportExport.Xml {
using System.Xml.Serialization;
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlRootAttribute("Zoo", Namespace="", IsNullable=false)]
public partial class ZooType {
private AnimalAbstractType[] animalsField;
///
[System.Xml.Serialization.XmlArrayItemAttribute("An imal", IsNullable = false)]
/* BEGIN MANUALLY ADDED */
[System.Xml.Serialization.XmlArrayItemAttribute("El ephant", typeof(ElephantType))]
[System.Xml.Serialization.XmlArrayItemAttribute("Ti ger", typeof(TigerType))]
[System.Xml.Serialization.XmlArrayItemAttribute("Mo nkey", typeof(MonkeyType))]
/* END MANUALLY ADDED */
public AnimalAbstractType[] Animals {
get {
return this.animalsField;
}
set {
this.animalsField = value;
}
}
}
///
[System.Xml.Serialization.XmlIncludeAttribute(typeo f(ElephantType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeo f(TigerType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeo f(MonkeyType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
public partial class AnimalAbstractType {
private string nameField;
///
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
}
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlRootAttribute("Monkey" , Namespace="", IsNullable=false)]
public partial class MonkeyType : AnimalAbstractType {
}
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlRootAttribute("Tiger", Namespace="", IsNullable=false)]
public partial class TigerType : AnimalAbstractType {
}
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlRootAttribute("Elephan t", Namespace="", IsNullable=false)]
public partial class ElephantType : AnimalAbstractType {
}
}
|