2017-05-06 3 views
0

ID와 같이 피드의 간단한 비트를 바인딩 할 수 있지만 복잡한 객체를 파싱하려고하면 항상 null이됩니다. 매핑에서 내가 뭘 잘못하고 있니?XML을 복잡한 객체에 비 직렬화

피드

[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; } 
} 

저자

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

항목

[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; } 
} 

직렬화 복원

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

표시 입력 XML. –

+2

[신디케이션 네임 스페이스] (https://msdn.microsoft.com/en-us/library/system.servicemodel.syndication (v = vs.110) .aspx)를 참조하십시오. RSS를 사용하려면 [SyndicationFeed Class] (https://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed (v = vs.110) .aspx)를 사용하십시오. –

+0

프로젝트가 .net 코어를 사용 중입니다. 입력 된 XML은 과 같은 블로그 피드 atom 피드입니다. http://cellularscale.blogspot.com/feeds/posts/default – Evr

답변

1

"피드"클래스 정의 비트가 의심됩니다.

아래의 방법으로 시도해보고 작동하는지 확인하십시오.

  1. "작성자"클래스 및 목록 클래스 public을 선언하십시오.
  2. 아래 코드와 같이 생성자에서 작성자 및 목록을 구성 해보십시오.

    [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! 피드의 작성자 및 항목에 공개를 추가하면 문제가 해결되었습니다. 고맙습니다 – Evr

관련 문제