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

1
  • Any reason you can't use LINQ here? Commented Nov 9, 2014 at 3:16

2 Answers 2

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.

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

2 Comments

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!
Jon Skeet gave a better answer to this than I will be able to provide you.
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

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.