Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is XmlSerializer's Deserialize() spitting out a child object which is a XmlNode[]?
    text
    copied!<p>I'm using <code>XmlSerializer</code> to serialize and then deserialize a simple object. When I deserialize the object to my surprise I find a child object was not properly deserialized but instead turned into <code>XmlNode[]</code>.</p> <p>Here is very nearly the structure I've got:</p> <pre><code>// This line I put in here as a way of sneaking into the XML the // root node's C# namespace, since it's not the same as the // deserializing code and the deserializing code seemed unable to // deserialize properly without knowing the Type (see my code below). // So I basically just use this fake construct to get the namespace // and make a Type of it to feed the XmlSerializer() instantiation. [XmlRoot(Namespace = "http://foo.com/CSharpNamespace/Foo.Bar")] // This is because QueuedFile can be given to the Argument array. [XmlInclude(typeof(QueuedFile))] // This class is Foo.Bar.CommandAndArguments public class CommandAndArguments { public String Command; public object[] Arguments; } // I don't think this matters to XmlSerialize, but just in case... [Serializable()] // I added this line just thinking maybe it would help, but it doesn't // do anything. I tried it without the XmlType first, and that // didn't work. [XmlType("Foo.Baz.Bat.QueuedFile")] // This class is Foo.Baz.Bat.QueuedFile (in a different c# // namespace than CommandAndArguments and the deserializing code) public QueuedFile { public String FileName; public String DirectoryName; } </code></pre> <p>And the code which deserializes it looks like:</p> <pre><code>public static object DeserializeXml(String objectToDeserialize) { String rootNodeName = ""; String rootNodeNamespace = ""; using (XmlReader xmlReader = XmlReader.Create(new StringReader(objectToDeserialize))) { if (xmlReader.MoveToContent() == XmlNodeType.Element) { rootNodeName = xmlReader.Name; rootNodeNamespace = xmlReader.NamespaceURI; if (rootNodeNamespace.StartsWith("http://foo.com/CSharpNamespace/")) { rootNodeName = rootNodeNamespace.Substring("http://foo.com/CSharpNamespace/".Length) + "." + rootNodeName; } } } //MessageBox.Show(rootNodeName); try { Type t = DetermineTypeFromName(rootNodeName); if (t == null) { throw new Exception("Could not determine type of serialized string. Type listed as: "+rootNodeName); } var s = new XmlSerializer(t); return s.Deserialize(new StringReader(objectToDeserialize)); // object o = new object(); // MethodInfo castMethod = o.GetType().GetMethod("Cast").MakeGenericMethod(t); // return castMethod.Invoke(null, new object[] { s.Deserialize(new StringReader(objectToDeserialize)) }); } catch (InvalidOperationException) { return null; } } </code></pre> <p>And here is the XML when the <code>CommandAndArguments</code> is serialized:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt; &lt;CommandAndArguments xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://foo.com/CSharpNamespace/Foo.Bar"&gt; &lt;Command&gt;I am a command&lt;/Command&gt; &lt;Arguments&gt; &lt;anyType xsi:type="Foo.Baz.Bat.QueuedFile"&gt; &lt;FileName xmlns=""&gt;HelloWorld.txt&lt;/FileName&gt; &lt;DirectoryName xmlns=""&gt;C:\foo\bar&lt;/DirectoryName&gt; &lt;/anyType&gt; &lt;/Arguments&gt; &lt;/CommandAndArguments&gt; </code></pre> <p>But when I deserialize I am given a <code>CommandAndArguments</code> object where Arguments is <code>XmlNode[]</code> with the first item being the attribute giving the QueuedFile as the type and the other indices being elements of the properties. But why wasn't the <code>QueuedFile</code> object recreated?</p> <p>I suspect this might somehow have do with C# namespaces and the engine doing the deserializing not being able to find or work with <code>QueuedFile</code>... But I don't see why since when I forgot the <code>XmlInclude()</code> it made sure to tell me it didn't expect <code>QueuedFile</code> and now that I've added the <code>XmlInclude()</code> I get no error, just an incomplete deserialization.</p> <p>Help? I've read everything I can find to read and Googled everything I know to Google and am stuck. I certainly have a lot to learn about XML serialization but I'm not sure how I'm failing at something which should be pretty simple (I actually did something almost exactly like this before without any problem, the only difference then was that everything was in the same C# namespace).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload