Is it possible to deserialize the following XML:
<MyObject><Test>Hi hello</Test><Something><Else><With><SubItems count='5'>hello world</SubItems></With></Else></Something></MyObject>
into this object:
public class MyObject {
public string Test { get; set; }
public string Something { get; set; }
}
with this as expected output (this fails at the moment with XmlException: Unexpected node type Element. ReadElementString method can only be called on elements with simple or empty content. Line 1, position 50.)
[TestMethod]
public void TestDeserialization()
{
var s = "<MyObject><Test>Hi hello</Test><Something><Else><With><SubItems count='5'>hello world</SubItems></With></Else></Something></MyObject>";
var o = s.DeSerialize<MyObject>();
Assert.AreEqual("Hi hello", o.Test);
Assert.AreEqual("<Else><With><SubItems count='5'>hello world</SubItems></With></Else>", o.Something);
}
public static class Xml
{
public static T DeSerialize<T>(this string xml) where T : new()
{
if (String.IsNullOrEmpty(xml))
{
return new T();
}
var xmlSer = new XmlSerializer(typeof(T));
using (var stream = new StringReader(xml))
return (T)xmlSer.Deserialize(stream);
}
}
DeSerializeso how can we possibly help?