Question XSD.exe generate class - choice element problem. please help

Brungle

New member
Joined
Sep 29, 2010
Messages
2
Programming Experience
Beginner
Hi,

I've been stuck on this for weeks now, I'm an inexperienced developer and unfortunately I've been landed with a large system that integrates with multiple web services.

A third pary we connect to has updated their xml schema and I've subsequently got an new XSD with various field changes. Unfortunately they have added a new choice element which I'm having great difficulty with. (their support team only develops in java so cant assist me)

schema code:
VB.NET:
- <xs:complexType name="Q1LandChargesBankruptcySearchType">
- <xs:annotation>
  <xs:documentation>Choice of Private Individual search or Complex Name search</xs:documentation> 
  </xs:annotation>
- <xs:sequence>
- <xs:choice>
  <xs:element name="BankruptcySearchPrivateIndividual" type="Q1BankruptcySearchPrivateIndividualType" /> 
  <xs:element name="BankruptcySearchComplexName" type="Q1BankruptcySearchComplexNameType" /> 
  </xs:choice>


Basically I've used the xsd.exe to generate a vb.net class which seems to be fine for the majority of fields except the choice element, the code generated is as below:

VB.NET:
        <System.Xml.Serialization.XmlElementAttribute("BankruptcySearchComplexName", GetType(Q1BankruptcySearchComplexNameType)), _
         System.Xml.Serialization.XmlElementAttribute("BankruptcySearchPrivateIndividual", GetType(Q1BankruptcySearchPrivateIndividualType))> _
        Public Property Item() As Object
            Get
                Return Me.itemField
            End Get
            Set(ByVal value As Object)
                Me.itemField = Value
            End Set
        End Property

it seems like a generic Item object? how can I manually change the generated code so it lets me select 'Q1BankruptcySearchComplexNameType' or 'Q1BankruptcySearchPrivateIndividual'.

I hope this makes sense. Any input would be greatly appreciated as this is causing me real problems and I really need to get these changes working before the old schema is disabled.. :(

thanks for reading
 
Brungle:

Were you able to resolve your question? I'm in the same boat you were and so far this is the best post I've found that matches what I'm trying to accomplish.

Jim
 
Hi,
Yes, well another developer did it for me, I can't recall exactly how at the moment but i will review the code when I'm back in the office and post it on here for you.

I think it was something to do with the fact that it was a choice element but it could be multiple types so that's why the xsd exe referenced it as a generic object initially.
 
Yeah, that's exactly what I'm dealing with.

The XSD contains an xs:choice with 2 non-similar types (1 string, 1 type definied later in the XSD). I have the XSD converted to a VB class, and that class included in the project. Now I'm trying to fill the data for the XML file, and not sure how to tell VB.Net how to "select" one of the choices.

If you could post what you did, that would be a huge help!
Thanks!
Jim
 
Resolved!

Ok, I've got this figured out now. I was trying to make it much more difficult than I needed to - I was trying to assign the choice value, THEN assign the elements for that particular choice.

Answer: Define the type in the code (if necessary), then assign the value to the .Item property. The XMLSerializer will encode the proper choice based on the type of the object assigned to the .Item property.


For anyone else that trips across this thread, here's an example (the idea here being to return a DateTime for the result set, along with either a. an error message; or b. a complexType containing various result data values):

The XSD file including a complexType in the xs:choice
VB.NET:
	<xs:element name="RootElementMixed">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="AsOf" type="xs:dateTime"/>
				<xs:choice>
					<xs:element name="ErrMsg" type="xs:string"/>
					<xs:element name="Results" type="Results"/>
				</xs:choice>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:complexType name="Results">
		<xs:sequence>
			<xs:element name="ID" type="xs:string"/>
			<xs:element name="Type" type="xs:integer"/>
			<xs:element name="Status" type="xs:dateTime"/>
		</xs:sequence>
	</xs:complexType>

The pertinent XSD.exe-generated VB class:
VB.NET:
Partial Public Class RootElementMixed
    Private asOfField As Date
    Private itemField As Object
    
    Public Property AsOf() As Date
        Get
            Return Me.asOfField
        End Get
        Set
            Me.asOfField = value
        End Set
    End Property
    
    <System.Xml.Serialization.XmlElementAttribute("ErrMsg", GetType(String)),  _
     System.Xml.Serialization.XmlElementAttribute("Results", GetType(Results))>  _
    Public Property Item() As Object
        Get
            Return Me.itemField
        End Get
        Set
            Me.itemField = value
        End Set
    End Property
End Class

And the code to "select" the assignments...
Option1 (string):
VB.NET:
      Dim REMx As New RootElementMixed

      REMx.AsOf = Now
      REMx.Item = "ErrorMessage"
Output:
VB.NET:
<?xml version="1.0" encoding="utf-16"?>
<RootElementMixed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <AsOf>2011-01-04T15:15:02.410948-07:00</AsOf>
  <ErrMsg>ErrorMessage</ErrMsg>
</RootElementMixed>

Option2 (complexType):
VB.NET:
      Dim REMx As New RootElementMixed
      Dim Results As New Results

      REMx.AsOf = Now

      Results.ID = "ResultsID"
      Results.Type = 0
      Results.Status = Now.AddHours(-1)
      REMx.Item = Results
Output:
VB.NET:
<?xml version="1.0" encoding="utf-16"?>
<RootElementMixed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <AsOf>2011-01-04T15:15:26.5822042-07:00</AsOf>
  <Results>
    <ID>ResultsID</ID>
    <Type>0</Type>
    <Status>2011-01-04T14:15:26.5822042-07:00</Status>
  </Results>
</RootElementMixed>



Note: this is not an issue when the XSD choices are for simple data types, in that case the XSD.exe-generated class will include something like "Private itemElementNameField As ItemChoiceType" to make the choice selection.


Reference: MSDN Choice Element Binding Support http://msdn.microsoft.com/en-us/library/sa6z5baz(v=vs.80).aspx
msdn.microsoft.com/en-us/library/sa6z5baz(v=vs.80).aspx
 
Back
Top