I have just started with XSD and tried to deserialize XML into a pretty
complex object which failed all the time. I have created the classes
with xsd. After two days of trying to figure out how this all works and
where the problem might be I finally concluded that this might be a
xsd.exe bug with creating code for sequences inside of a choice. But as
I am novice in these matters I thought I better ask...
I created a simple xsd from scratch for testing like that:
-------------- snippet 1 begins -----------------------
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="XMLSchema1"
targetNamespace="http://tempuri.org/XMLSchema1.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema1.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mytop">
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element name="test1" type="xs:string" />
<xs:element name="test2" type="xs:string" />
</xs:sequence>
<xs:sequence>
<xs:element name="test3" type="xs:string" />
<xs:element name="test4" type="xs:string" />
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
-------------- snippet 1 ends -----------------------
xsd.exe /classes generates for this xsd:
-------------- snippet 2 begins -----------------------
using System.Xml.Serialization;
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://tempuri.org/XMLSchema1.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="http://tempuri.org/XMLSchema1.xsd",
IsNullable=false)]
public class mytop {
[System.Xml.Serialization.XmlElementAttribute("test 2", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("test 3", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("test 1", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("test 4", typeof(string))]
[System.Xml.Serialization.XmlChoiceIdentifierAttrib ute("ItemElementName")]
public string Item;
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName;
}
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://tempuri.org/XMLSchema1.xsd",
IncludeInSchema=false)]
public enum ItemChoiceType {
test2,
test3,
test1,
test4,
}
-------------- snippet 2 ends -----------------------
The generated code looks like xsd basically ignored the sequences and
just put all content elements from the sequence into the choice giving
me the choice between test1-4 instead of the choice of groups test1+2
and test3+4.
Am I missing here something and did something wrong or is this actually
an incorrectly generated c# class? In the latter case, is there any good
option (or other generators) to get it working without changing the xsd?
Thanks a lot!
Regards,
Gerald