我已经设法使用 XML 序列化序列化 Word 类型的对象列表:
public static void WriteXML(string fileName)
{
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(typeof(Word));
System.IO.StreamWriter file = new System.IO.StreamWriter(
fileName);
foreach (var word in Words)
{
writer.Serialize(file, word);
}
file.Close();
}
我在反序列化此列表时遇到问题。我使用这个代码片段:http: //msdn.microsoft.com/en-us/library/vstudio/ms172872.aspx
我将我的代码更改为:
public static void ReadXML(string fileName)
{
System.Xml.Serialization.XmlSerializer reader =
new System.Xml.Serialization.XmlSerializer(typeof(Word));
System.IO.StreamReader file = new System.IO.StreamReader(
fileName);
foreach (????)
{
Word word=new Word();
word = (Word) reader.Deserialize(file);
Words.Add(word); //Words is a List<Word>
}
}
当然,这里没有正确使用 foreach() 循环。我只是不知道该怎么做。