3
我想反序列化一个XML,但是我有很多问题,我正在使用List对象。XML列表的问题<T> C#中的反序列化
的XML如下:
<EntriesSerialize>
<Entries>
<Entry file="myFile"
value="2000" />
<Entry file="myFile"
value="400" />
<Entry file="myFile"
value="200" />
</Entries>
</EntriesSerialize>
我的类别是:
[XmlType("Entry")]
public class Entry
{
public Entry()
{
}
[XmlAttribute("file")]
public string File { get; set; }
[XmlAttribute("value")]
public string Value { get; set; }
}
[XmlType("EntriesSerialize")]
public class EntriesSerialize
{
public EntriesSerialize()
{
EntriesList = new List<Entry>();
}
[XmlElement("Entries")]
public List<Entry> EntriesList { get; set; }
}
,我使用反序列化这个XML的代码是:
public static T Deserialize<T>(string content)
{
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(content);
MemoryStream stream = new MemoryStream(byteArray);
StreamReader reader = new StreamReader(stream);
XmlSerializer serializer;
serializer = new XmlSerializer(typeof(T));
T model = (T)serializer.Deserialize(reader);
reader.Close();
return model;
}
的方法我在反序列化中调用的是反序列化(字符串内容),如下所示:
EntriesSerialize temp = Deserialize<EntriesSerialize>(data);
但是,当我使用C#调试器查看temp变量时,发现EntriesList对象中只有一个元素,并且此元素的文件和值属性为null。
注意:这些类的序列化按预期工作。