8

I'm trying to deserialize the following xml into an Object. Xml got multiple namespaces. I tried to deserialize the Xml into an object. The object (data) has a reference to the LastChannel Object. But when i ask for data.channel which should give me the LastChannel, i get a nullpointer.

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>

The objects look like this:

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

and

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

Anybody sees why the data.channel ist null?

Serializer:

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

1 Answer 1

5

Your channel is in the default xmlns, viz http://purl.org/rss/1.0/

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

You'll also need to correct the date formats e.g. 2013-04-15**T**13:27:06

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.