0

好的,我有一个这样的 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本书。有任何想法吗?

4

3 回答 3

4

如果您稍微更改反序列化代码,它将起作用。

(参见XmlRootAttribute& 和 type的用法Book[]

XmlSerializer serializer = new XmlSerializer(typeof(Book[]),new XmlRootAttribute("Books"));
FileStream fs = new FileStream(@"Book.xml", FileMode.Open);

using (XmlReader reader = XmlReader.Create(fs))
{
    var books = (Book[])serializer.Deserialize(reader);
}

顺便说一句:您不需要Serializable属性,它仅由BinaryFormatter使用

PS:你也可以使用类型List<Book>代替Book[]

编辑

稍微修改一下代码后,它可以很简单:

XmlSerializer serializer = new XmlSerializer(typeof(List<Book>),
                                              new XmlRootAttribute("Books"));

using (FileStream fs = new FileStream(@"Book.xml", FileMode.Open))
{
    List<Book> books = (List<Book>)serializer.Deserialize(fs);
}
于 2013-07-27T15:08:30.490 回答
1

您需要创建一个包含书籍列表和 seriazlie 这个类的类:

[Serializable]
public class BookData
{
  [XmlArray(ElementName="Books")]
  [XmlArrayItem(ElementName="Book")]
  public List<Book> Books {get; set;}

}

然后像这样创建序列化程序:

var serializer = new XmlSerializer(typeof(BookData));
于 2013-07-27T14:56:30.637 回答
0

FileStream我们可以从 book 元素中读取和迭代而不是从中加载,这XmlDataDocument将使我们的序列化变得更加容易。请看一下修改后的方法。

List<Book> BookList = new List<Book>();

            XmlDataDocument objDocument = new XmlDataDocument();
            objDocument.Load(@"Book.xml");


            XmlSerializer serializer = new XmlSerializer(typeof(Book));                

            foreach(XmlNode objItem in   objDocument.DocumentElement.ChildNodes)
            {
                TextReader objTextReader = new StringReader(objItem.OuterXml);

                using (XmlReader reader = XmlReader.Create(objTextReader))
                    {
                        BookList.Add((Book)serializer.Deserialize(reader));
                        reader.Close();
                    }
            }
于 2013-07-27T15:11:02.910 回答