I am unable to deserialize from serialized XML. Below is the code. I do know that if I completely remove the xmlns attributes the XML is then well forms and can be deserialized.
Class XML derives from:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace MyNamespace
{
[DataContract(Name = "MyClass", Namespace = "")]
public class MyClass
{
public MyClass()
{
}
public MyClass(string _Total2, string _Total1)
{
Total2 = _Total2;
Total1 = _Total1;
}
[DataMember(Name = "Total2", EmitDefaultValue = false)]
public string Total2 { get; set; }
[DataMember(Name = "Total1", EmitDefaultValue = false)]
public string Total1 { get; set; }
}
}
XML output:
<GetResponse xmlns=\"http://tempuri.org/\"><GetResult xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Total1 xmlns=\"\">100</Total1><Total2 xmlns=\"\">200</Total2 ></GetResult></GetResponse>
Deserialization method:
public static T DeserializeFromXml<T>(string xml)
{
T result;
var ser = new XmlSerializer(typeof(T));
using (TextReader tr = new StringReader(xml))
{
result = (T)ser.Deserialize(tr);
}
return result;
}
Class XML derives from:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace MyNamespace
{
[DataContract(Name = "MyClass", Namespace = "")]
public class MyClass
{
public MyClass()
{
}
public MyClass(string _Total2, string _Total1)
{
Total2 = _Total2;
Total1 = _Total1;
}
[DataMember(Name = "Total2", EmitDefaultValue = false)]
public string Total2 { get; set; }
[DataMember(Name = "Total1", EmitDefaultValue = false)]
public string Total1 { get; set; }
}
}
XML output:
<GetResponse xmlns=\"http://tempuri.org/\"><GetResult xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Total1 xmlns=\"\">100</Total1><Total2 xmlns=\"\">200</Total2 ></GetResult></GetResponse>
Deserialization method:
public static T DeserializeFromXml<T>(string xml)
{
T result;
var ser = new XmlSerializer(typeof(T));
using (TextReader tr = new StringReader(xml))
{
result = (T)ser.Deserialize(tr);
}
return result;
}