Deserializing array of objects from XML.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im trying to deserialize some XML. It looks like this:
<div style="color:black; background-color:white
<pre> <span style="color:blue <<span style="color:#a31515 ArrayOfAnyType <span style="color:red xmlns:xsi<span style="color:blue =<span style="color:black "<span style="color:blue http://www.w3.org/2001/XMLSchema-instance<span style="color:black " <span style="color:red xmlns:xsd<span style="color:blue =<span style="color:black "<span style="color:blue http://www.w3.org/2001/XMLSchema<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 anyType <span style="color:red xsi:type<span style="color:blue =<span style="color:black "<span style="color:blue xsd:int<span style="color:black "<span style="color:blue >100<span style="color:blue </<span style="color:#a31515 anyType<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 anyType <span style="color:red xsi:type<span style="color:blue =<span style="color:black "<span style="color:blue xsd:int<span style="color:black "<span style="color:blue >200<span style="color:blue </<span style="color:#a31515 anyType<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 anyType <span style="color:red xsi:type<span style="color:blue =<span style="color:black "<span style="color:blue xsd:boolean<span style="color:black "<span style="color:blue >true<span style="color:blue </<span style="color:#a31515 anyType<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 anyType <span style="color:red xsi:type<span style="color:blue =<span style="color:black "<span style="color:blue xsd:string<span style="color:black "<span style="color:blue >foobar<span style="color:blue </<span style="color:#a31515 anyType<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 ArrayOfAnyType<span style="color:blue >

[/code]

The original thing that was serialized was an object[] , and of course, thats what Id like to get back when Deserializing.
So the whole thing is a subtree in a larger XML document.
Ive tried to deserialize it like this:
<div style="color:black; background-color:white
<pre>XmlSerializer s = <span style="color:blue new XmlSerializer( t, compatibleTypes );
XmlNodeReader reader = <span style="color:blue new XmlNodeReader( node );
<span style="color:blue object result = s.Deserialize( reader );
<span style="color:blue return result;
[/code]

t is typeof(object[]) and compatibleTypes is an array of Types containing
typeof(int), typeof(bool), and typeof(string) . node is the XmlNode of the XmlDocument that is the element <ArrayOfAnyType>.
The resulting object array that gets deserialized is not what I expected. It is, in fact, of type
object[] , and it has the right number of elements, (4 in my example) but each element is not the object of the type specified by the anyType element. Instead each element of this object[] array is an array of 2 XmlNodes (System.Xml.XmlNode[2])
which are as follows:
+<span style="white-space:pre [0]<span style="white-space:pre
{Attribute, Name="xsi:type", Value="xsd:int"}<span style="white-space:pre
System.Xml.XmlNode {System.Xml.XmlAttribute}
+<span style="white-space:pre [1]<span style="white-space:pre
{Text, Value="100"}<span style="white-space:pre System.Xml.XmlNode {System.Xml.XmlText}
So again, thats an object[4] of XmlNode[2]s.
What I was actually expecting was an object[4] containing objects of type int, int, bool, string respectively. But this didnt happen.
My question is: how the heck did it deserialize XmlNodes instead of the actual objects? And how can I get it to deserialize to basically one of these: new object[] { 100, 200, true, "foobar" }

Update:
I think this is possible because http://skysigal.xact-solutions.com/Blog/tabid/427/entryid/1870/C-Using-the-XmlSerializer-to-serialize-arrays-that-contain-custom-classes.aspx
this post seems to claim that it is possible, at least for simple types. But the reason for having it return the XmlNodes is when it cant resolve the namespaces. XML namespaces is, admittedly, a weak point of mine. So maybe I did something
wrong in setting this up. Maybe its not recognizing the xsi and xsd namespaces somehow because of the way I created my XmlReader by way of an XmlNodeReader. ? (Im guessing. This is my question.)
I should reiterate that the xml document is larger, in that the above xml is contained in a node. There are no other namespace nodes in the document though. Its relatively simple:

<pre lang="x-xml <simple>
<thing>
<ArrayOfAnyType ...
</thing>
</simple>[/code]
<br/>
<br/>


View the full article
 
Back
Top