开发者

Blocking problem Deserializing XML to object problem

I have a blocking problem I have XML file under some url

http://myserver/mywebApp/myXML.xml

In the below code which I run in Console Application, bookcollection has null Books field :(


<books>
<book id="5352">
<date>1986-05-05</date>
<title>
Alice in chains
</title>
 </book>
<book id="4334">
<date>1986-05-05</date>
<title>
1000 ways to heaven
</title>
 </book>
<book id="1111">
<date>1986-05-05</date>
<title>
Kitchen and me
</title>
 </book>
</books>
XmlDocument doc = new XmlDocument(); 
doc.Load("http://myserver/mywebapp/myXML.xml");
BookCollection books = new BookCollection();

XmlNodeReader reader2 = new XmlNodeReader(doc.DocumentElement);
XmlSerializer ser2 = new XmlSerializer(books.GetType());
object obj = ser2.Deserialize(reader2);

BookCollection books2=   (BookCollection)obj; 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    [Serializable()]
      public class Book
        {
             [System.Xml.Serialization.XmlAttribute("id")]
            public string id { get; set; }

             [System.Xml.Serialization.XmlElement("date")]
            public string date { get; set; }

              [System.Xml.Serialization.XmlElement("title")]
            public string title { get; set; }


        }
}





using Syst开发者_开发百科em;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    [Serializable()]
    [System.Xml.Serialization.XmlRootAttribute("books", Namespace = "", IsNullable = false)]
    public class BookCollection
    {
        [XmlArray("books")]
        [XmlArrayItem("book", typeof(Book))]
        public Book[] Books { get; set; }
    }
} 


That appears to expect /books/books/book. Try instead:

    [XmlElement("book")]
    public Book[] Books { get; set; }

(no array/array-item)


Marc is 100% correct, by changing the attribute you use on the Books array you can correctly deserialize the XML. One thing I also want to point out is when you construct your XmlSerializer like this

BookCollection books = new (); 
XmlSerializer ser2 = new XmlSerializer(books.GetType()); 

It is not neccesary to new an instance of BookCollection to get the type. You should rather be using typeof

XmlSerializer ser2 = new XmlSerializer(typeof(BookCollection)); 

And as per Marc's point the complete BookCollection class should look like this

[System.Xml.Serialization.XmlRootAttribute("books", Namespace = "", IsNullable = false)]  
public class BookCollection  
{  
    [System.Xml.Serialization.XmlElement("book")]  
    public Book[] Books { get; set; }  
}

Putting the XmlElement attribute on an Array or any of the serializeable collections like List and List<T> basically tells the serializer you want the elements of the array serialized as child elements of the current element ie. since you are providing the element via the BooksCollection class you just want the array serialized as child elements of the BooksCollection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜