Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Deserialization with multiple namespaces

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;
}
like image 213
flo1411 Avatar asked Apr 04 '14 10:04

flo1411


People also ask

Can XML have multiple namespaces?

When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.

Can I make XmlSerializer ignore the namespace on Deserialization?

Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization. Note this is the kind of thing I meant. You are not telling the XmlSerializer to ignore namespaces - you are giving it XML that has no namespaces.

Is a namespace for XML serialization?

Serialization namespace contains several Attribute classes that can be applied to members of a class. For example, if a class contains a member that will be serialized as an XML element, you can apply the XmlElementAttribute attribute to the member.

What is Deserialization XML?

Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization, on the other hand, is used to convert the byte of data, such as XML or binary data, to object type.


1 Answers

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

like image 149
StuartLC Avatar answered Oct 07 '22 09:10

StuartLC