2014-04-04 4 views
3

다음 XML을 deserialize하여 Object에 넣으려고합니다. XML에 여러 개의 네임 스페이스가 있습니다. XML을 개체로 deserialize하려고했습니다. 개체 (데이터)에는 LastChannel 개체에 대한 참조가 있습니다. 그러나 내가 LastChannel을 제공해야하는 data.channel을 요청하면 nullpointer가 발생합니다.XML 여러 개의 네임 스페이스가있는 비 직렬화

XML을 :

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
     xmlns="http://purl.org/rss/1.0/" 
     xmlns:dc="http://purl.org/dc/elements/1.1/" 
     xmlns:mp="http://www.tagesschau.de/rss/1.0/modules/metaplus/" 
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" 
     xmlns:content="http://purl.org/rss/1.0/modules/content/"> 

<channel> 
<title>title</title> 
<description>Default description</description> 
<dc:date>2013-04-15 13:27:06</dc:date> 
<sy:updateBase>2013-04-15 13:27:06</sy:updateBase> 
<sy:updatePeriod>hourly</sy:updatePeriod> 
<sy:updateFrequency>12</sy:updateFrequency> 
</channel> 
</rdf:RDF> 

오브젝트는 다음과 같이 :

[XmlRoot("RDF", Namespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#")] 
public class LastRss 
{ 
    [XmlElement("channel")] 
    public LastChannel channel { get; set; } 
} 

public class LastChannel 
{ 
    [XmlElement("title")] 
    public string title { get; set; } 
    [XmlElement("description")] 
    public string description { get; set; } 
    [XmlElement("date", Namespace = "http://purl.org/dc/elements/1.1/")] 
    public DateTime date { get; set; } 
    [XmlElement("updateBase", Namespace = "http://purl.org/rss/1.0/modules/syndication/")] 
    public DateTime updateBase { get; set; } 
    [XmlElement("updatePeriod", Namespace = "http://purl.org/rss/1.0/modules/syndication/")] 
    public string updatePeriod { get; set; } 
    [XmlElement("updateFrequency", Namespace = "http://purl.org/rss/1.0/modules/syndication/")] 
    public int updateFrequency { get; set; } 
} 

누구는 왜 data.channel 인도 표준시 널 (null)을보고?

시리얼 : 또한 날짜 형식 예를 수정해야합니다

LastRss data = new LastRss(); 
XmlSerializer serializer = new XmlSerializer(typeof(LastRss)); 
System.IO.TextReader reader = new System.IO.StringReader(xml); 
try 
{ 
    object o = serializer.Deserialize(reader); 
    data = (LastRss)o; 
} 

답변

3

귀하의 채널이 http://purl.org/rss/1.0/

[XmlElement("channel", Namespace = "http://purl.org/rss/1.0/")] 
    public LastChannel channel { get; set; } 

즉, 기본 xmlns2013-04-15**T**13:27:06

+1

완벽! 고마워요! – flo1411

관련 문제