473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Deserialization and casting problems

I am consuming a web service and using the generated Reference.cs to access
the service and the objects associated with it.

I have run into a problem where some inherited classes are not being
deserialized. I have verified that the XML being returned by the service
contains the tags I am expecting, but they don't show up in the resulting
object. Here's the relevant portion of the Reference.cs file:

[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
public class FormSection {
[System.Xml.Serialization.XmlElementAttribute("fiel d")]
public FormField[] field;
}
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
[System.Xml.Serialization.XmlIncludeAttribute(typeo f(SelectOneField))]
[System.Xml.Serialization.XmlIncludeAttribute(typeo f(SelectMultipleField))]
[System.Xml.Serialization.XmlIncludeAttribute(typeo f(BooleanField))]
public class FormField {
[System.Xml.Serialization.XmlElementAttribute("valu e")]
public string[] value;
[System.Xml.Serialization.XmlAttributeAttribute()]
public FormFieldType type;
}
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
public enum FormFieldType {
boolean,
select1,
selectn,
}
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
[System.Xml.Serialization.XmlIncludeAttribute(typeo f(SelectMultipleField))]
public class SelectOneField : FormField {
[System.Xml.Serialization.XmlArrayItemAttribute("ch oice")]
public Choice[] choices;
}
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
public class Choice {
public string value;
[System.Xml.Serialization.XmlArrayItemAttribute("fi eld")]
public FormField[] fields;
}
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
public class SelectMultipleField : SelectOneField {
[System.Xml.Serialization.XmlAttributeAttribute()]
public int maximum_choices;
[System.Xml.Serialization.XmlAttributeAttribute()]
public int minimum_choices;
}
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
public class BooleanField : FormField {
[System.Xml.Serialization.XmlArrayItemAttribute("fi eld")]
public FormField[] fields;
}

The problem occurs when I get XML like this:

<field type="select1">
<value/>
<choices>
<choice>
<value>17</value>
<fields>
</fields>
</choice>
<choice>
<value>18</value>
<fields>
</fields>
</choice>
</choices>

When this is deserialized, there are no choices! And in code, if I try to
cast
to one of the inherited types, I get an error. Here's the snippet of code:

FormField field = <stuff>.field[0];
switch (field.type)
{
case FormFieldType.boolean:
string[] boolean_value = {"on"};
field.value = boolean_value;
break;
case FormFieldType.select1:
SelectOneField sel1 = (SelectOneField) field;
string[] select1_value = {sel1.choices[0].value};
field.value = select1_value;
}

The cast to SelectOneField fails with "Specified cast is not valid." Why???

Am I doing something wrong? Is there a bug in the service somewhere?

Any help appreciated.

Thanks,

-- Greg
Mar 7 '06 #1
5 2267

"Greg Allen" <ga****@arrayinc.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP12.phx.gbl...
I am consuming a web service and using the generated Reference.cs to access
the service and the objects associated with it.

I have run into a problem where some inherited classes are not being
deserialized. I have verified that the XML being returned by the service
contains the tags I am expecting, but they don't show up in the resulting
object. Here's the relevant portion of the Reference.cs file:
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
public class FormSection {
[System.Xml.Serialization.XmlElementAttribute("fiel d")]
public FormField[] field;
}

[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
[System.Xml.Serialization.XmlIncludeAttribute(typeo f(SelectOneField))]

[System.Xml.Serialization.XmlIncludeAttribute(typeo f(SelectMultipleField))]
[System.Xml.Serialization.XmlIncludeAttribute(typeo f(BooleanField))]
public class FormField {
[System.Xml.Serialization.XmlElementAttribute("valu e")]
public string[] value;
[System.Xml.Serialization.XmlAttributeAttribute()]
public FormFieldType type;
}

[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
public enum FormFieldType {
boolean,
select1,
selectn,
}

[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]

[System.Xml.Serialization.XmlIncludeAttribute(typeo f(SelectMultipleField))]
public class SelectOneField : FormField {
[System.Xml.Serialization.XmlArrayItemAttribute("ch oice")]
public Choice[] choices;
}

[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
public class Choice {
public string value;
[System.Xml.Serialization.XmlArrayItemAttribute("fi eld")]
public FormField[] fields;
}

[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
public class SelectMultipleField : SelectOneField {
[System.Xml.Serialization.XmlAttributeAttribute()]
public int maximum_choices;
[System.Xml.Serialization.XmlAttributeAttribute()]
public int minimum_choices;
}

[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
public class BooleanField : FormField {
[System.Xml.Serialization.XmlArrayItemAttribute("fi eld")]
public FormField[] fields;
}

The problem occurs when I get XML like this:

<field type="select1">
<value/>
<choices>
<choice>
<value>17</value>
<fields>
</fields>
</choice>
<choice>
<value>18</value>
<fields>
</fields>
</choice>
</choices>

When this is deserialized, there are no choices! And in code, if I try to
cast
to one of the inherited types, I get an error. Here's the snippet of
code:

FormField field = <stuff>.field[0];
switch (field.type)
{
case FormFieldType.boolean:
string[] boolean_value = {"on"};
field.value = boolean_value;
break;
case FormFieldType.select1:
SelectOneField sel1 = (SelectOneField) field;
string[] select1_value = {sel1.choices[0].value};
field.value = select1_value;
}

The cast to SelectOneField fails with "Specified cast is not valid."
Why???

Am I doing something wrong? Is there a bug in the service somewhere?

Any help appreciated.

Thanks,

-- Greg


I guess your WSDL define "choices" as a sequence of "choice".

I have the same problem.

It seems to be a bug in the code generator when he find a sequence
containing only one type: the generated code define an array of "choice",
but nothing to handle the "choices" level.

At run time an exception is raised during de-serialization stating a
"choices" was found where a "choice" was expected, and no deserialization
occurs.
You may try to use a list instead of a sequence (i have not tried that).

I found an ugly workaround: add a second field named "dummy" in the
definition of choices and make this field optional (minOccurs="0").

The generated code is completly different (no more arrays), but now correct.

Rémy
Mar 7 '06 #2
OK, here's the relevant section from the WSDL file:

<s:complexType name="FormSection">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="field"
type="tns:FormField" />
</s:sequence>
</s:complexType>
<s:complexType name="FormField">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="value"
type="s:string" />
</s:sequence>
<s:attribute name="type" type="tns:FormFieldType" use="required" />
</s:complexType>
<s:simpleType name="FormFieldType">
<s:restriction base="s:string">
<s:enumeration value="boolean" />
<s:enumeration value="select1" />
<s:enumeration value="selectn" />
</s:restriction>
</s:simpleType>
<s:complexType name="BooleanField">
<s:complexContent mixed="false">
<s:extension base="tns:FormField">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="fields"
type="tns:ArrayOfFormField" />
</s:sequence>
</s:extension>
</s:complexContent>
</s:complexType>
<s:complexType name="ArrayOfFormField">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="field"
nillable="true" type="tns:FormField" />
</s:sequence>
</s:complexType>
<s:complexType name="SelectOneField">
<s:complexContent mixed="false">
<s:extension base="tns:FormField">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="choices"
type="tns:ArrayOfChoice" />
</s:sequence>
</s:extension>
</s:complexContent>
</s:complexType>
<s:complexType name="ArrayOfChoice">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="choice"
nillable="true" type="tns:Choice" />
</s:sequence>
</s:complexType>
<s:complexType name="Choice">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="value"
type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="fields"
type="tns:ArrayOfFormField" />
</s:sequence>
</s:complexType>
<s:complexType name="SelectMultipleField">
<s:complexContent mixed="false">
<s:extension base="tns:SelectOneField">
<s:attribute name="maximum_choices" type="s:int" use="required"
/>
<s:attribute name="minimum_choices" type="s:int" use="required"
/>
</s:extension>
</s:complexContent>
</s:complexType>

I put in dummy elements as suggested, like this

<s:element minOccurs="0" maxOccurs="1" name="dummy"
type="s:string" />

wherever there was a sequence with only 1 type. That changed the resulting
file,
but the deserialization still didn't give me the choices.

Am I still missing something? How can I catch the exception during
deserialization that
you describe, to see if I am having the same problem? Is this a known bug?

Thanks again,

-- Greg
Mar 7 '06 #3
The XmlSerializer can deal with choices ok - though the code
interpretation is a little messy:
http://msdn.microsoft.com/msdnmag/is...3/06/XMLFiles/

How did you generate your schema?

Josh
http://www.thejoyofcode.com/

Mar 8 '06 #4

"Greg Allen" <ga****@arrayinc.com> a écrit dans le message de news:
OT**************@tk2msftngp13.phx.gbl...
OK, here's the relevant section from the WSDL file:

<s:complexType name="FormSection">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="field"
type="tns:FormField" />
</s:sequence>
</s:complexType>
<s:complexType name="FormField">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="value"
type="s:string" />
</s:sequence>
<s:attribute name="type" type="tns:FormFieldType" use="required" />
</s:complexType>
<s:simpleType name="FormFieldType">
<s:restriction base="s:string">
<s:enumeration value="boolean" />
<s:enumeration value="select1" />
<s:enumeration value="selectn" />
</s:restriction>
</s:simpleType>
<s:complexType name="BooleanField">
<s:complexContent mixed="false">
<s:extension base="tns:FormField">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="fields"
type="tns:ArrayOfFormField" />
</s:sequence>
</s:extension>
</s:complexContent>
</s:complexType>
<s:complexType name="ArrayOfFormField">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="field"
nillable="true" type="tns:FormField" />
</s:sequence>
</s:complexType>
<s:complexType name="SelectOneField">
<s:complexContent mixed="false">
<s:extension base="tns:FormField">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="choices"
type="tns:ArrayOfChoice" />
</s:sequence>
</s:extension>
</s:complexContent>
</s:complexType>
<s:complexType name="ArrayOfChoice">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="choice"
nillable="true" type="tns:Choice" />
</s:sequence>
</s:complexType>
<s:complexType name="Choice">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="value"
type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="fields"
type="tns:ArrayOfFormField" />
</s:sequence>
</s:complexType>
<s:complexType name="SelectMultipleField">
<s:complexContent mixed="false">
<s:extension base="tns:SelectOneField">
<s:attribute name="maximum_choices" type="s:int" use="required"
/>
<s:attribute name="minimum_choices" type="s:int" use="required"
/>
</s:extension>
</s:complexContent>
</s:complexType>

I put in dummy elements as suggested, like this

<s:element minOccurs="0" maxOccurs="1" name="dummy"
type="s:string" />

wherever there was a sequence with only 1 type. That changed the
resulting file,
but the deserialization still didn't give me the choices.

There is maybe another problem then ?
You should try to run your code in debugger an try to trace where the
deserialization fails.
Am I still missing something? How can I catch the exception during
deserialization that
you describe, to see if I am having the same problem? Is this a known
bug?

I have seen the exception when running in debugger. It seems to be catched
before returning to our code.

I don't know if it's a known bug (or a misunderstanding on my side) ...
Thanks again,

-- Greg

Mar 8 '06 #5
I've think I've found a workaround, which may point to the actual problem.

If I change this:

[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
public class FormSection {
[System.Xml.Serialization.XmlElementAttribute("fiel d")]
public FormField[] field;
}

to this:

[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://mywebservice.com/1.0")]
public class FormSection {
[System.Xml.Serialization.XmlElementAttribute("fiel d")]
public SelectMultipleField[] field;
}

It deserializes correctly!

It seems to have something to do with the inheritance. SelectMultipleField
is
the "top most" in the inheritance chain.

And I don't appear to be getting an exception raised during deserialization.
I've tried
to catch it, but it doesn't appear that anything is being thrown.

Does this maybe shed some light on what the real problem might be?

-- Greg
Mar 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sean McFee | last post by:
I am using a C# project in .NET and am running into a problem with deserialization. Previously I have serialized and deserialized these objects without any problems. However I recently moved some...
1
by: Linus | last post by:
Hi, I'm having problems with some very simple deserialization code and would appreciate it very much if I could get some help here. The following is the code:...
4
by: Mike Sarbu | last post by:
Hello all, I have an XML file like this: <?xml version="1.0" encoding="utf-8"?> <SomeObject xmlns="http://www.abcinc.com/objectdefinition"...
3
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database...
3
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the...
5
by: frustratedWithDotNet | last post by:
Why does .NET not issue messages or throw exceptions if it doesn't like something in the response from a web service?? I am getting a response object, but an array of custom objects within the...
7
by: PeterW | last post by:
I have an xml file containing some stuff. I use xsd to generate a schema and again to create classes from the schemas. added using System, System.IO; added to all classes added namespace ROC...
1
by: Gregor D. | last post by:
Hi, I want to consume an Apache Webservice with a C# .NET Client. Everything works fine, but I'm having problem with the deserialization of the complex type "Item" that is included in the...
1
by: Kapps | last post by:
Hi, I was wondering how I would go about deserializing a file that was serialized from a different project. I've tried a couple things, but I can't really seem to figure it out. When looking it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.