asitis
asitis

Reputation: 3031

How to parse xml link tag href attribute using c#

This is the sample xml of a feed item

 <item>
   <pubDate>2013-12-11 10:28:55</pubDate>
   <title>
     SAG Awards Nominations: 12 Years a Slave, Breaking Bad lead the race
   </title>
   <link>
     http://www.rottentomatoes.com/m/1929182/news/1929182/
   </link>
   <description>
   <![CDATA[ ]]>
   </description>
   <atom:link rel="thumbnail" type="image/*"  href="http://content6.flixster.com/movie/11/17/36/11173600_tmb.jpg"/>
  </item>

c# code for parsing xml elements

 List<XElement> elementsList = xmlItems.Descendants("item").ToList();
 foreach (XElement rssItem in elementsList)
 {
    RSSItem rss = new RSSItem();
    rss.Description1 = rssItem.Element("description").Value;
    rss.Link1 = rssItem.Element("link").Value;
    rss.Title1 = rssItem.Element("title").Value;
    rss.ImageUrl= ;
 }

I successfully parsed the xml elements except the atom:link tag url.
How we can parse the href property from the atom:link tag ?

Upvotes: 1

Views: 3045

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500365

You need to specify the namespace when you look for the element:

XNamespace atom = "http://www.w3.org/2005/Atom";
...
rss.Link1 = rssItem.Element(atom + "link").Attribute("href").Value;

LINQ to XML makes namespace handling much simpler than any other XML API I've seen, but you still need to be aware of it. (I'm surprised the other elements aren't in a namespace, to be honest.)

I'd also transform your foreach loop into a LINQ query:

var items = xmlItems.Descendants("item")
                    .Select(x => new RSSItem {
                         Description1 = x.Element("description").Value,
                         Link1 = x.Element(atom + "link").Attribute("href").Value,
                         Title1 = x.Element("title").Value,
                         ...
                     })
                    .ToList();

Also consider using a cast to string instead of the Value property, if some of the elements may be missing - that will set the relevant property to null, instead of throwing a NullReferenceException.

EDIT: If the link element is missing, you can fix that with:

Link1 = (string) x.Elements(atom + "link").Attributes("href").FirstOrDefault()

That will find the first href attribute within an atom link element, or use null - and then the cast to string will just return null if there's no attribute. (That's part of the user-defined conversion from XAttribute to string.)

Upvotes: 1

Kevin Gosse
Kevin Gosse

Reputation: 39007

Link has a namespace, you need to indicate it when parsing the XML. I don't remember exactly what namespace atom is, but it should be indicated somewhere in the XML file (usually on the root node). For instance, if it is:

<feed xmlns:atom="http://www.w3.org/2005/Atom">

Then you need to parse it like this:

rss.Link1 = (string)rssItem.Element(XName.Get("link", "http://www.w3.org/2005/Atom")).Attribute("href");

Upvotes: 2

Related Questions