XSD.exe and Choice Elements.
-
Given the following XSD File Sequence Choice Element Singular Item (Doesn't have to be a reference) Collection of Items (Must be a Reference) As you can see there is a **choice element ** which has a choice of either a singular Item or a collection of Items. When you use XSD.exe to create the classes in .net you get the following: /// [System.Xml.Serialization.XmlElementAttribute("Collection", typeof(Item[]))] [System.Xml.Serialization.XmlElementAttribute("Item", typeof(Item))] public object Item { get { return this.itemField; } set { this.itemField = value; } } So .NET xsd.exe recognises it’s a choice element and creates the Serialization correctly. The problem occurs when you try and create a “WebReference” to the web service in a client application. If you look at the reference.cs it has an incorrect version: /// [System.Xml.Serialization.XmlElementAttribute("Collection", typeof(ArrayOfItemItem[]))] [System.Xml.Serialization.XmlElementAttribute("Item", typeof(TestXSDResultItem))] public object Item { get { return this.itemField; } set { this.itemField = value; } } As a result if you try and consume the web service by setting the choice element to a collection and returning that through the WS back into the client application you’ll end up with an empty collec
-
Given the following XSD File Sequence Choice Element Singular Item (Doesn't have to be a reference) Collection of Items (Must be a Reference) As you can see there is a **choice element ** which has a choice of either a singular Item or a collection of Items. When you use XSD.exe to create the classes in .net you get the following: /// [System.Xml.Serialization.XmlElementAttribute("Collection", typeof(Item[]))] [System.Xml.Serialization.XmlElementAttribute("Item", typeof(Item))] public object Item { get { return this.itemField; } set { this.itemField = value; } } So .NET xsd.exe recognises it’s a choice element and creates the Serialization correctly. The problem occurs when you try and create a “WebReference” to the web service in a client application. If you look at the reference.cs it has an incorrect version: /// [System.Xml.Serialization.XmlElementAttribute("Collection", typeof(ArrayOfItemItem[]))] [System.Xml.Serialization.XmlElementAttribute("Item", typeof(TestXSDResultItem))] public object Item { get { return this.itemField; } set { this.itemField = value; } } As a result if you try and consume the web service by setting the choice element to a collection and returning that through the WS back into the client application you’ll end up with an empty collec
I'm not sure off hand from your description if this will help or not, but I have a tool that builds code from XSD, but in the case of a Choice, I generate a property for each choice on the class. The code generated is XML serializable. You might want to grab a copy of the trial and see if it does what you need. You can try it on-line here[^], or you can download the trial here[^] Hope that helps.
Try code model generation tools at BoneSoft.com.
-
Given the following XSD File Sequence Choice Element Singular Item (Doesn't have to be a reference) Collection of Items (Must be a Reference) As you can see there is a **choice element ** which has a choice of either a singular Item or a collection of Items. When you use XSD.exe to create the classes in .net you get the following: /// [System.Xml.Serialization.XmlElementAttribute("Collection", typeof(Item[]))] [System.Xml.Serialization.XmlElementAttribute("Item", typeof(Item))] public object Item { get { return this.itemField; } set { this.itemField = value; } } So .NET xsd.exe recognises it’s a choice element and creates the Serialization correctly. The problem occurs when you try and create a “WebReference” to the web service in a client application. If you look at the reference.cs it has an incorrect version: /// [System.Xml.Serialization.XmlElementAttribute("Collection", typeof(ArrayOfItemItem[]))] [System.Xml.Serialization.XmlElementAttribute("Item", typeof(TestXSDResultItem))] public object Item { get { return this.itemField; } set { this.itemField = value; } } As a result if you try and consume the web service by setting the choice element to a collection and returning that through the WS back into the client application you’ll end up with an empty collec
Got a fix from Microsoft in the end. Horah! FIX: A deserialized object may contain null objects when you deserialize an object from XML in the .NET Framework 2.0 http://support.microsoft.com/kb/918298