2017-05-06 5 views
0

Ich bin in der Lage, einfache Informationsbits wie ID aus dem Feed zu binden, aber der Versuch, komplexe Objekte zu analysieren, endet immer null. Was mache ich falsch in meinem Mapping?XML in komplexe Objekte deserialisieren

-Feed

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

    [XmlElement("entry")] 
    List<Entry> Entries { get; set; } 

    [XmlElement("id")] 
    public string Id { get; set; } 
} 

Autor

[XmlType("author")] 
public class Author 
{ 
    [XmlElement("name")] 
    public string Name { get; set; } 
    [XmlElement("email")] 
    public string Email { get; set; } 
} 

Eintrag

[XmlType("entry")] 
public class Entry 
{ 
    [XmlElement("id")] 
    public string Id { get; set; } 
    [XmlElement("published")] 
    DateTime Published { get; set; } 
    [XmlElement("updated")] 
    DateTime Updated { get; set; } 
    [XmlElement("title")] 
    public string Title { get; set; } 
} 

Deserialisieren

 using (Stream stream = res.GetResponseStream()) 
     { 
      XmlSerializer serializer = new XmlSerializer(typeof(Feed)); 
      feed = (Feed)serializer.Deserialize(stream); 
     } 
+1

anzeigen xml-Eingang. –

+2

Siehe [Syndication Namespace] (https://msdn.microsoft.com/en-us/library/system.servicemodel.syndication (v = vs.110) .aspx). Verwenden Sie [SyndicationFeed Class] (https://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed (v = vs.110) .aspx), um mit RSS zu arbeiten. –

+0

Projekt verwendet .net-Kern, Eingabe-XML ist blogspot Atom-Feed wie http://cellularscale.blogspot.com/feeds/posts/default – Evr

Antwort

1

Ich vermute "feed" class definition bit.

versuchen Sie den folgenden Weg und sehen, ob es funktioniert.

  1. Deklarieren Sie Ihre "Author" -Klasse und List-Klasse public.
  2. Versuchen Sie, Autor und Liste im Konstruktor zu erstellen, wie im folgenden Code gezeigt.

    [XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")] 
    public class Feed 
    { 
         //ADD A CONSTRUCTOR AND CREATE LIST AND AUTHOR 
         public Feed() 
         { 
         Author1 = new Author(); 
         Entries = new List<Entry>(); 
    
         } 
    
         [XmlElement("author")] 
         public Author Author1 { get; set; } 
    
         [XmlElement("entry")] 
         public List<Entry> Entries { get; set; } 
    
         [XmlElement("id")] 
         public string Id { get; set; } 
    } 
    
+0

Argh! Das Hinzufügen von public zu Author und Entries in Feed behob das Problem. Vielen Dank – Evr