473,670 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
public class FormSection {
[System.Xml.Seri alization.XmlEl ementAttribute( "field")]
public FormField[] field;
}
[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
[System.Xml.Seri alization.XmlIn cludeAttribute( typeof(SelectOn eField))]
[System.Xml.Seri alization.XmlIn cludeAttribute( typeof(SelectMu ltipleField))]
[System.Xml.Seri alization.XmlIn cludeAttribute( typeof(BooleanF ield))]
public class FormField {
[System.Xml.Seri alization.XmlEl ementAttribute( "value")]
public string[] value;
[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public FormFieldType type;
}
[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
public enum FormFieldType {
boolean,
select1,
selectn,
}
[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
[System.Xml.Seri alization.XmlIn cludeAttribute( typeof(SelectMu ltipleField))]
public class SelectOneField : FormField {
[System.Xml.Seri alization.XmlAr rayItemAttribut e("choice")]
public Choice[] choices;
}
[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
public class Choice {
public string value;
[System.Xml.Seri alization.XmlAr rayItemAttribut e("field")]
public FormField[] fields;
}
[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
public class SelectMultipleF ield : SelectOneField {
[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public int maximum_choices ;
[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public int minimum_choices ;
}
[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
public class BooleanField : FormField {
[System.Xml.Seri alization.XmlAr rayItemAttribut e("field")]
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.b oolean:
string[] boolean_value = {"on"};
field.value = boolean_value;
break;
case FormFieldType.s elect1:
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 2289

"Greg Allen" <ga****@arrayin c.com> a écrit dans le message de news:
%2************* ***@TK2MSFTNGP1 2.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.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
public class FormSection {
[System.Xml.Seri alization.XmlEl ementAttribute( "field")]
public FormField[] field;
}

[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
[System.Xml.Seri alization.XmlIn cludeAttribute( typeof(SelectOn eField))]

[System.Xml.Seri alization.XmlIn cludeAttribute( typeof(SelectMu ltipleField))]
[System.Xml.Seri alization.XmlIn cludeAttribute( typeof(BooleanF ield))]
public class FormField {
[System.Xml.Seri alization.XmlEl ementAttribute( "value")]
public string[] value;
[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public FormFieldType type;
}

[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
public enum FormFieldType {
boolean,
select1,
selectn,
}

[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]

[System.Xml.Seri alization.XmlIn cludeAttribute( typeof(SelectMu ltipleField))]
public class SelectOneField : FormField {
[System.Xml.Seri alization.XmlAr rayItemAttribut e("choice")]
public Choice[] choices;
}

[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
public class Choice {
public string value;
[System.Xml.Seri alization.XmlAr rayItemAttribut e("field")]
public FormField[] fields;
}

[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
public class SelectMultipleF ield : SelectOneField {
[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public int maximum_choices ;
[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public int minimum_choices ;
}

[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
public class BooleanField : FormField {
[System.Xml.Seri alization.XmlAr rayItemAttribut e("field")]
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.b oolean:
string[] boolean_value = {"on"};
field.value = boolean_value;
break;
case FormFieldType.s elect1:
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:complexTyp e name="FormSecti on">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbo unded" name="field"
type="tns:FormF ield" />
</s:sequence>
</s:complexType>
<s:complexTyp e name="FormField ">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbo unded" name="value"
type="s:string" />
</s:sequence>
<s:attribute name="type" type="tns:FormF ieldType" use="required" />
</s:complexType>
<s:simpleType name="FormField Type">
<s:restrictio n base="s:string" >
<s:enumeratio n value="boolean" />
<s:enumeratio n value="select1" />
<s:enumeratio n value="selectn" />
</s:restriction>
</s:simpleType>
<s:complexTyp e name="BooleanFi eld">
<s:complexConte nt mixed="false">
<s:extension base="tns:FormF ield">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="fields"
type="tns:Array OfFormField" />
</s:sequence>
</s:extension>
</s:complexConten t>
</s:complexType>
<s:complexTyp e name="ArrayOfFo rmField">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbo unded" name="field"
nillable="true" type="tns:FormF ield" />
</s:sequence>
</s:complexType>
<s:complexTyp e name="SelectOne Field">
<s:complexConte nt mixed="false">
<s:extension base="tns:FormF ield">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="choices"
type="tns:Array OfChoice" />
</s:sequence>
</s:extension>
</s:complexConten t>
</s:complexType>
<s:complexTyp e name="ArrayOfCh oice">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbo unded" name="choice"
nillable="true" type="tns:Choic e" />
</s:sequence>
</s:complexType>
<s:complexTyp e 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:Array OfFormField" />
</s:sequence>
</s:complexType>
<s:complexTyp e name="SelectMul tipleField">
<s:complexConte nt mixed="false">
<s:extension base="tns:Selec tOneField">
<s:attribute name="maximum_c hoices" type="s:int" use="required"
/>
<s:attribute name="minimum_c hoices" type="s:int" use="required"
/>
</s:extension>
</s:complexConten t>
</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****@arrayin c.com> a écrit dans le message de news:
OT************* *@tk2msftngp13. phx.gbl...
OK, here's the relevant section from the WSDL file:

<s:complexTyp e name="FormSecti on">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbo unded" name="field"
type="tns:FormF ield" />
</s:sequence>
</s:complexType>
<s:complexTyp e name="FormField ">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbo unded" name="value"
type="s:string" />
</s:sequence>
<s:attribute name="type" type="tns:FormF ieldType" use="required" />
</s:complexType>
<s:simpleType name="FormField Type">
<s:restrictio n base="s:string" >
<s:enumeratio n value="boolean" />
<s:enumeratio n value="select1" />
<s:enumeratio n value="selectn" />
</s:restriction>
</s:simpleType>
<s:complexTyp e name="BooleanFi eld">
<s:complexConte nt mixed="false">
<s:extension base="tns:FormF ield">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="fields"
type="tns:Array OfFormField" />
</s:sequence>
</s:extension>
</s:complexConten t>
</s:complexType>
<s:complexTyp e name="ArrayOfFo rmField">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbo unded" name="field"
nillable="true" type="tns:FormF ield" />
</s:sequence>
</s:complexType>
<s:complexTyp e name="SelectOne Field">
<s:complexConte nt mixed="false">
<s:extension base="tns:FormF ield">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="choices"
type="tns:Array OfChoice" />
</s:sequence>
</s:extension>
</s:complexConten t>
</s:complexType>
<s:complexTyp e name="ArrayOfCh oice">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbo unded" name="choice"
nillable="true" type="tns:Choic e" />
</s:sequence>
</s:complexType>
<s:complexTyp e 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:Array OfFormField" />
</s:sequence>
</s:complexType>
<s:complexTyp e name="SelectMul tipleField">
<s:complexConte nt mixed="false">
<s:extension base="tns:Selec tOneField">
<s:attribute name="maximum_c hoices" type="s:int" use="required"
/>
<s:attribute name="minimum_c hoices" type="s:int" use="required"
/>
</s:extension>
</s:complexConten t>
</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 misunderstandin g 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.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
public class FormSection {
[System.Xml.Seri alization.XmlEl ementAttribute( "field")]
public FormField[] field;
}

to this:

[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://mywebservice.co m/1.0")]
public class FormSection {
[System.Xml.Seri alization.XmlEl ementAttribute( "field")]
public SelectMultipleF ield[] field;
}

It deserializes correctly!

It seems to have something to do with the inheritance. SelectMultipleF ield
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
867
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 classes from one assembly to another. This is causing the serialization routine to be unable to resolve the types saved with the old version of the code. The error I get is: Type not resolved for member Foo.Bar.Class1, OldAssembly,...
1
1547
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: ========================================================= XmlReader reader = new XmlTextReader("test.xml"); XmlValidatingReader vr = new XmlValidatingReader(reader);
4
3151
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" xmlns:someobj=http://www.abcinc.com/objectdefinition> ...... </SomeObject>
3
9792
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 (when I check the database the field that holds the binary data seems populated), but when I want to deserialize this same byte array the application fails and gives me the following error: Binary stream does not contain a valid BinaryHeader, 0...
3
9791
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 response. As a result of seraching news groups I guessed that the SOAP response defines an array element in a way that causes the dotnet deserialization routines to put the content in a generic object array (object) BUT the content is supposed to...
5
5528
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 response is null instead of being populated. The SOAP response from the service looks good and I cannot see anything wrong with the WSDL or XML schema. How do I get .NET to tell me what it doesn't like? Is there a way to turn on some kind of tracing...
7
1794
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 added deserialize() method as follows to AWLR2 class: public AWLR2 deserialize(string xmlString)
1
2663
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 response of the web service, what seems so be the type "hashtable" in .NET. Every "Item" consists of two fields "key" and "value". I read a lot about SOAP/Apache/.NET/Complex Types/etc. and somewhere I read that there is no automatic deserialization...
1
1367
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 up, it seemed like it would be solved by just including a class project that did the actual serializing and deserializing, but when I tried this, I still couldn't get it to work. I created a DLL that was referenced by both of my projects, the server...
0
8466
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8384
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8896
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8590
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7410
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2798
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.