This is the XML file I'm trying to deserialize:
<?xml version="1.0" encoding="utf-8"?>
<d:MyItem xmlns:d="http://someurl" xmlns:m="http://someotherurl">This is a string</d:MyItem>
The xsd tool generated the following class:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://someurl")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://someurl", IsNullable=false)]
public partial class MyItem {
private object[] itemsField;
/// <remarks/>
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
I'm currently trying to deserialize the same xml that xsd
used to generate the class:
var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<d:MyItem xmlns:d=\"http://someurl\" xmlns:m=\"http://someotherurl\">This is a string</d:MyItem>";
var deserialized = Deserialize<MyItem>(xml);
Where Deserialize<>
is:
private static T Deserialize<T>(string xml)
{
var xmlDocument = XDocument.Parse(xml);
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
return (T)serializer.Deserialize(xmlDocument.CreateReader());
}
The problem is that although Deserialize
returns an instance (not null), the Items
property inside it is null
i.e. it's not being deserialized.
How am I able to get the string from inside this XML?