好的,我有一个这样的 XML 文件:
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book>
<Name>book name</Name>
<Url>book url</Url>
<Genre>book genre</Genre>
<City>book city</City>
<Country>book country</Country>
</Book>
<Book>
<Name>book name</Name>
<Url>book url</Url>
<Genre>book genre</Genre>
<City>book city</City>
<Country>book country</Country>
</Book>
<Book>
<Name>book name</Name>
<Url>book url</Url>
<Genre>book genre</Genre>
<City>book city</City>
<Country>book country</Country>
</Book>
</Books>
我想用Deserialization
它来装满List
书。我有这样的课:
[Serializable]
public class Book
{
public string Name{get; set;}
public string Url { get; set; }
public string Genre { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
像List
这样:
List<Book> BookList;
我尝试像这样从 xml 文件中读取:
XmlSerializer serializer = new XmlSerializer(typeof(Book));
FileStream fs = new FileStream(@"Book.xml", FileMode.Open);
using (XmlReader reader = XmlReader.Create(fs))
{
BookList.Add((Book)serializer.Deserialize(reader));
reader.Close();
}
但是每当我运行我的应用程序时,我都会在 Xml 文件中遇到错误。如果我将我的 xml 文件更改为此:
<Book>
<Name>book name</Name>
<Url>book url</Url>
<Genre>book genre</Genre>
<City>book city</City>
<Country>book country</Country>
</Book>
一切顺利,希望我只读一本书。我想做的是阅读大约100本书。有任何想法吗?