2012-03-08 5 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です。 2つのエントリが表示されますが、すべてがnullです。私は気づく

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'として返します –

関連する問題