0
我想反序列化並讀取一個簡單的XML文件,下面的代碼完成沒有錯誤,但反序列化沒有執行。XML反序列化問題
我的消息有一個節點,下面有三個元素。在Deserialize
調用之後,我試圖讀取其中一個屬性,它是空的。
public class Program
{
private string response = @"<?xml version=""1.0""?>
<ApplicationException xmlns = ""http://schemas.datacontract.org/2004/07/System"" xmlns:x=""http://www.w3.org/2001/XMLSchema"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">
<Name xmlns = """" i:type=""x:string"">System.ApplicationException</Name>
<Message xmlns = """" i:type=""x:string"">Error Message 12345</Message>
<DataPoints xmlns = """" i:nil=""true""/>
</ApplicationException>";
private XElement xElement;
private ExceptionClass theException;
public TestXML()
{
xElement = XElement.Parse(response);
var xmlSerializer = new XmlSerializer(typeof(ExceptionClass));
using (var reader = xElement.CreateReader())
{
theException = (ExceptionClass)xmlSerializer.Deserialize(reader);
}
Console.WriteLine(theException.Message); // nothing is showing up
}
}
[XmlRoot(Namespace = "http://schemas.datacontract.org/2004/07/System",
ElementName = "ApplicationException")]
public class ExceptionClass
{
public String Name { get; set; }
public String Message { get; set; }
public String DataPoints { get; set; }
}
捷克這一點:http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document –