Null value node is causing deserialization problems

Y

yads12

Let's say I have the following wsdl snipet:

<s:complexType name="consumer_response_info">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="final_score"
type="tns:final_score" />
<s:element minOccurs="0" maxOccurs="1" name="app_score"
type="tns:app_score" />
<s:element minOccurs="0" maxOccurs="1" name="num_blank_chars"
type="tns:num_blank_chars" />
</s:sequence>
</s:complexType>
<s:complexType name="final_score" mixed="true">
<s:sequence>
<s:any minOccurs="0" maxOccurs="1" />
</s:sequence>
</s:complexType>
<s:complexType name="app_score" mixed="true">
<s:sequence>
<s:any minOccurs="0" maxOccurs="1" />
</s:sequence>
</s:complexType>
<s:complexType name="num_blank_chars" mixed="true">
<s:sequence>
<s:any minOccurs="0" maxOccurs="1" />
</s:sequence>
</s:complexType>

Which generates the following class
public partial class consumer_response_info {
private System.Xml.XmlNode final_scoreField;
private System.Xml.XmlNode app_scoreField;

/// <remarks/>
public System.Xml.XmlNode final_score {
get {
return this.final_scoreField;
}
set {
this.final_scoreField = value;
}
}

/// <remarks/>
public System.Xml.XmlNode app_score {
get {
return this.app_scoreField;
}
set {
this.app_scoreField = value;
}
}

/// <remarks/>
public System.Xml.XmlNode num_blank_chars {
get {
return this.num_blank_charsField;
}
set {
this.num_blank_charsField = value;
}
}
}

If the following xml is passed from the webservice:
<consumer_response_info>
<final_score>998</final_score>
<app_score>100</app_score>
<num_blank_chars>0</num_blank_chars>
</consumer_response_info>

everything works fine and is deserialized properly.

If however the final_score element is null:
<consumer_response_info>
<final_score />
<app_score>100</app_score>
<num_blank_chars>0</num_blank_chars>
</consumer_response_info>

The deserialization comes back with the object having app_score and
num_blank_chars values of null and the OuterXml value of final_score is
"<app_score>100</app_score>". So it seems to just ignore the null value
of final_score and populate the next node (in this case app_score) as
an element inside the final_score node. Incidently it also fails to
parse anymore of the document.

Furthermore, final_score, app_score, and num_blank_chars have their own
types (with the same name) on the server side, for some reason the
Visual Studio autogenerated classes mark them as type XmlNode. Not sure
if that's important or not.

Does anybody know why it's failing like this and any potential
solutions to the problem?
 
J

John Saunders

Let's say I have the following wsdl snipet:

<s:complexType name="consumer_response_info">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="final_score"
type="tns:final_score" />
<s:element minOccurs="0" maxOccurs="1" name="app_score"
type="tns:app_score" />
<s:element minOccurs="0" maxOccurs="1" name="num_blank_chars"
type="tns:num_blank_chars" />
</s:sequence>
</s:complexType>
<s:complexType name="final_score" mixed="true">
<s:sequence>
<s:any minOccurs="0" maxOccurs="1" />
</s:sequence>
</s:complexType>
<s:complexType name="app_score" mixed="true">
<s:sequence>
<s:any minOccurs="0" maxOccurs="1" />
</s:sequence>
</s:complexType>
<s:complexType name="num_blank_chars" mixed="true">
<s:sequence>
<s:any minOccurs="0" maxOccurs="1" />
</s:sequence>
</s:complexType>

Which generates the following class
public partial class consumer_response_info {
private System.Xml.XmlNode final_scoreField;
private System.Xml.XmlNode app_scoreField;

/// <remarks/>
public System.Xml.XmlNode final_score {
get {
return this.final_scoreField;
}
set {
this.final_scoreField = value;
}
}

/// <remarks/>
public System.Xml.XmlNode app_score {
get {
return this.app_scoreField;
}
set {
this.app_scoreField = value;
}
}

/// <remarks/>
public System.Xml.XmlNode num_blank_chars {
get {
return this.num_blank_charsField;
}
set {
this.num_blank_charsField = value;
}
}
}

If the following xml is passed from the webservice:
<consumer_response_info>
<final_score>998</final_score>
<app_score>100</app_score>
<num_blank_chars>0</num_blank_chars>
</consumer_response_info>

everything works fine and is deserialized properly.

If however the final_score element is null:
<consumer_response_info>
<final_score />
<app_score>100</app_score>
<num_blank_chars>0</num_blank_chars>
</consumer_response_info>

The deserialization comes back with the object having app_score and
num_blank_chars values of null and the OuterXml value of final_score is
"<app_score>100</app_score>". So it seems to just ignore the null value
of final_score and populate the next node (in this case app_score) as
an element inside the final_score node. Incidently it also fails to
parse anymore of the document.

Furthermore, final_score, app_score, and num_blank_chars have their own
types (with the same name) on the server side, for some reason the
Visual Studio autogenerated classes mark them as type XmlNode. Not sure
if that's important or not.

Does anybody know why it's failing like this and any potential
solutions to the problem?

xs:any is causing XmlNode.

I don't know if it's your null problem, but why have mixed="true" if you've
only got elements inside and no text?

John
 
Y

yads12

John said:
xs:any is causing XmlNode.

I don't know if it's your null problem, but why have mixed="true" if you've
only got elements inside and no text?

John

This is the autogenerated wsdl file from the webservice that we
created. We do only have text inside the elements. Regardless why would
xmlnode deserialize the xml incorrectly?
 
J

John Saunders

This is the autogenerated wsdl file from the webservice that we
created. We do only have text inside the elements. Regardless why would
xmlnode deserialize the xml incorrectly?

I didn't say that it should do the wrong thing, only that mixed="true" might
be what was instigating it to do so.

You might try removing the mixed attribute just to see what happens...

John
 
Y

yads12

I fixed the problem on the webservice end. Those classes all had an
Item property that was an XmlNode and had an XmlAnyElementAttribute. I
changed the XmlAnyElementAttribute to
XmlElementAttribute(IsNullable=true) and the deserialization now works
correctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top