2012-03-08 18 views
2

什麼是使用C#反序列化以下XML的正確方法?無法反序列化XML與解包的集合

<?xml version='1.0' encoding='UTF-8'?> 
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gCal='http://schemas.google.com/gCal/2005'> 
    <id>http://www.google.com/cal...</id> 
    <subtitle type='text'>RockPointe Events (1)</subtitle> 
    <entry> 
     <id>http://www.google.com/cal...</id> 
     <published>2011-07-07T21:43:44.000Z</published> 
     <updated>2011-07-07T21:48:31.000Z</updated> 
     <title type='html'>Event 1</title> 
     <summary type='html'>Event 1 Summary</summary> 
     <content type='html'>Event 1 Content</content> 
    </entry> 
    <entry> 
     <id>http://www.google.com/cal...</id> 
     <published>2011-07-07T21:43:44.000Z</published> 
     <updated>2011-07-07T21:48:31.000Z</updated> 
     <title type='html'>Event 2</title> 
     <summary type='html'>Event 2 Summary</summary> 
     <content type='html'>Event 2 Content</content> 
    </entry> 
</feed> 

這是我目前的POCO

[XmlRoot(ElementName = "feed", Namespace = "http://www.w3.org/2005/Atom")] 
    public class Feed 
    { 
     [XmlElement("subtitle")] 
     public string Subtitle { get; set; } 

     [XmlElement("title")] 
     public string Title { get; set; } 

     [XmlElement("entry")] 
     public m_Entry[] Entry { get; set; } 

     [XmlType(Namespace = "")] 
     public class m_Entry 
     { 
      [XmlElement("title")] 
      public string Title { get; set; } 

      [XmlElement("summary")] 
      public string Summary { get; set; } 

      [XmlElement("content")] 
      public string Content { get; set; } 

      [XmlElement("published")] 
      public DateTime Published { get; set; } 

      [XmlElement("updated")] 
      public DateTime Updated { get; set; } 
     } 
    } 

當我通過我的Deserialize方法運行它,我得到TitleSubtitle預期。問題出在entry。我得到兩個條目,但一切都是空的。

null entry

回答

2

,看來它是從入門級

[XmlRoot(ElementName = "feed", Namespace = "http://www.w3.org/2005/Atom")] 
public class Feed 
{ 
    [XmlElement("subtitle")] 
    public string Subtitle { get; set; } 

    [XmlElement("title")] 
    public string Title { get; set; } 

    [XmlElement("entry")] 
    public Entry[] Entries { get; set; } 

    public class Entry 
    { 
     [XmlElement("title")] 
     public string Title { get; set; } 

     [XmlElement("summary")] 
     public string Summary { get; set; } 

     [XmlElement("content")] 
     public string Content { get; set; } 

     [XmlElement("published")] 
     public DateTime Published { get; set; } 

     [XmlElement("updated")] 
     public DateTime Updated { get; set; } 
    } 
} 
0

有一兩件事我注意到的是,你反序列化entry陣列作爲一個單一的元素,而不是一個數組。也許你應該考慮改變裝飾的東西沿着線:嗯

[XmlArray("feed")] 
[XmlArrayItem("entry", typeof(m_Entry))] 
public m_Entry[] entry { get; set; } 
+0

我想你說的去除XmlType屬性一樣容易,但是不是一個'null'入口屬性的數組(我上面有),你只是返回一個'entry'對象作爲'null' – 2012-03-09 00:21:51