【发布时间】:2012-05-24 18:13:49
【问题描述】:
我无法解决这个问题,我有一个看起来像这样的 xml 表
<root>
<list id="1" title="One">
<word>TEST1</word>
<word>TEST2</word>
<word>TEST3</word>
<word>TEST4</word>
<word>TEST5</word>
<word>TEST6</word>
</list>
<list id="2" title="Two">
<word>TEST1</word>
<word>TEST2</word>
<word>TEST3</word>
<word>TEST4</word>
<word>TEST5</word>
<word>TEST6</word>
</list>
</root>
我正在尝试将其序列化为
public class Items
{
[XmlAttribute("id")]
public string ID { get; set; }
[XmlAttribute("title")]
public string Title { get; set; }
//I don't know what to do for this
[Xml... something]
public list<string> Words { get; set; }
}
//I don't this this is right either
[XmlRoot("root")]
public class Lists
{
[XmlArray("list")]
[XmlArrayItem("word")]
public List<Items> Get { get; set; }
}
//Deserialize XML to Lists Class
using (Stream s = File.OpenRead("myfile.xml"))
{
Lists myLists = (Lists) new XmlSerializer(typeof (Lists)).Deserialize(s);
}
我是 XML 和 XML 序列化的新手,非常感谢任何帮助
【问题讨论】:
-
对 Words 属性使用 XmlArray
-
请注意,如果您要将 XML 转换为对象,那就是反序列化。将对象转换为 XML(或任何其他可以发送到磁盘或网络流的格式)正在序列化。