I know that this question has already been asked, but I have strange problem and I can't figure out what to do:
public static class XmlHelper
{
public static T Deserialize<T>(string xml)
{
using (var sr = new StringReader(xml))
{
var xs = new XmlSerializer(typeof(T));
return (T)xs.Deserialize(sr);
}
}
public static string Serialize(object o)
{
using (var sw = new StringWriter())
{
using (var xw = XmlWriter.Create(sw))
{
var xs = new XmlSerializer(o.GetType());
xs.Serialize(xw, o);
return sw.ToString();
}
}
}
}
[Serializable]
public class MyClass
{
public string Property1 {get;set;}
public int Property2 {get;set;}
}
I'm serializing class:
var a = XmlHelper.Serialize(new MyClass{ Property1 = "a", Property2 = 3 });
var b = XmlHelper.Deserialize<object>(a);
Error: There is an error in XML document (1, 41).
Edit: I want to deserialize a as object, is it possible?