0

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

Kirkland
  • 798
  • 1
  • 8
  • 20

2 Answers2

2

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.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • What other benefits of this method over the one I was trying do you see? Do you feel that this is the best way to parse XML in most cases? When would you use this one over the other? I'm sorry, I know I sound like a questionnaire, but I really like to learn. Any response would be greatly appreciated! – Kirkland Nov 09 '14 at 17:38
  • 1
    Jon Skeet gave [a better answer](http://stackoverflow.com/questions/1542073/xdocument-or-xmldocument) to this than I will be able to provide you. – Jonesopolis Nov 09 '14 at 18:21
0

FYI the Apple web site says that the itunes namespace link is case sensitive. I haven't used the version="2.0" part yet but so far I haven't needed it. I was using a link that I copied from elsewhere that was "...DTDs/Podcast-1.0.dtd". Only after changing it to lower case did the parsing in my RSS reader start to work.

Screenshot of Apple documentation

Gary Z
  • 155
  • 2
  • 6