I'm trying to parse a iTunes podcast XML feed in C# and am having trouble. It successfully downloads the feed and puts it into the XmlDocument object (tested). After that, it goes to the for-each line, but never enters the loop. I have no idea why it's saying that there aren't any elements in channel/item (at least that's what I'm thinking at this time). Heres the code:
string _returnedXMLData;
XmlDocument _podcastXmlData = new XmlDocument();
public List<PodcastItem> PodcastItemsList = new List<PodcastItem> ();
_podcastXmlData.Load(@"http://thepointjax.com/Podcast/podcast.xml");
string title = string.Empty;
string subtitle = string.Empty;
string author = string.Empty;
foreach (XmlNode node in _podcastXmlData.SelectNodes(@"channel/item")) {
title = node.SelectSingleNode (@"title").InnerText;
subtitle = node.SelectSingleNode (@"itunes:subtitle").InnerText;
author = node.SelectSingleNode (@"itunes:author").InnerText;
PodcastItemsList.Add (new PodcastItem(title, subtitle, author));
}
}
Thank you in advance for any assistance! It's much appreciated!
Kirkland
Going off my comment, I'd just use XDocument
:
var xml = XDocument.Load("http://thepointjax.com/Podcast/podcast.xml");
XNamespace ns = "http://www.itunes.com/dtds/podcast-1.0.dtd";
foreach (var item in xml.Descendants("item"))
{
var title = item.Element("title").Value;
var subtitle = item.Element(ns + "subtitle").Value;
var author = item.Element(ns + "author").Value;
PodcastItemsList.Add (new PodcastItem(title, subtitle, author));
}
itunes
is a namespace in the XML, so you need to use an XNamespace
to account for it.