2

I'm trying to figure out a way to retrieve the itunes elements from this xml feed and can not for the life of me figure it out.

<item>
    <title>Episode 41 - Brobdingnagian Lunches To Die For</title>
    <pubDate>Fri, 17 Jul 2015 13:00:00 GMT</pubDate>
    <dcterms:modified>2015-07-17</dcterms:modified>
    <dcterms:created>2015-07-17</dcterms:created>
    <link>http://starttocontinue.podomatic.com</link>
    <dc:creator>Start To Continue</dc:creator>
    <itunes:duration>0</itunes:duration>
    <itunes:explicit>yes</itunes:explicit>
    <itunes:order>1</itunes:order>
    ...
</item>

I'm using a standard for each loop to set the easy elements like title etc.

foreach (SyndicationItem item in feed.Items)
{
    string subject = item.Title.Text;
    ...
}

But don't know how to access the itunes ones, does anyone have an idea if this is possible or how to do it?

Daniel
  • 9,491
  • 12
  • 50
  • 66
Spitfire2k6
  • 308
  • 4
  • 17
  • Look into [LINQ-to-XML](https://msdn.microsoft.com/en-us/library/bb387098.aspx) which allow you to parse & extract information from arbitrary XML structure. [This is an example](http://stackoverflow.com/questions/319591/reading-non-standard-elements-in-a-syndicationitem-with-syndicationfeed) for similar scenario. Btw, there is no *attribute* in your XML sample, they are all *elements* (opening tag+content+closing tag=element) – har07 Jul 20 '15 at 12:16
  • @har07 I saw that one but didn't know how to access the particular item in my loop. – Spitfire2k6 Jul 20 '15 at 13:18
  • 1
    I'm not familiar with `SyndicationFeed` to give you an example from top of my head and I can't try without having a simplified but complete RSS feed XML sample. Anyway, have you looked at [this answer](http://stackoverflow.com/a/1768359/2998271) specifically? that answer looks promising for your case – har07 Jul 20 '15 at 13:27
  • This works :D Cheers mate. – Spitfire2k6 Jul 20 '15 at 13:49

4 Answers4

1

You could use ElementExtensions property to get additional elements:

const string ns = "http://www.itunes.com/dtds/podcast-1.0.dtd";
var duration = item.ElementExtensions.ReadElementExtensions<XElement>("duration", ns)
                   .Select(e => e.Value).First();
user202472
  • 166
  • 1
  • 6
0

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication37
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
            "<item xmlns:dcterms=\"www.myspace.com\" xmlns:dc=\"www.myspace.com\" xmlns:itunes=\"www.myspace.com\">" +
            "<title>Episode 41 - Brobdingnagian Lunches To Die For</title>" +
            "<pubDate>Fri, 17 Jul 2015 13:00:00 GMT</pubDate>" +
            "<dcterms:modified>2015-07-17</dcterms:modified>" +
            "<dcterms:created>2015-07-17</dcterms:created>" +
            "<link>http://starttocontinue.podomatic.com</link>" +
            "<dc:creator>Start To Continue</dc:creator>" +
            "<itunes:duration>0</itunes:duration>" +
            "<itunes:explicit>yes</itunes:explicit>" +
            "<itunes:order>1</itunes:order>" +
            "</item>";

            XDocument doc = XDocument.Parse(input);

            var results = doc.Descendants("item").Select(x => new
            {
                title = x.Element("title").Value,
                pubDate = x.Element("pubDate").Value,
                modified = x.Elements().Where(y => y.Name.LocalName == "modified").FirstOrDefault().Value,
                created = x.Elements().Where(y => y.Name.LocalName == "created").FirstOrDefault().Value,
                link = x.Element("link").Value,
                creator = x.Elements().Where(y => y.Name.LocalName == "creator").FirstOrDefault().Value,
                duration = x.Elements().Where(y => y.Name.LocalName == "duration").FirstOrDefault().Value,
                explicit1 = x.Elements().Where(y => y.Name.LocalName == "explicit").FirstOrDefault().Value,
                order = x.Elements().Where(y => y.Name.LocalName == "order").FirstOrDefault().Value,
            }).ToList();
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

This ended up working.

XElement ele = extension.GetObject<XElement>();
if (ele.ToString().StartsWith("<itunes:image"))
    {
      int index = ele.ToString().IndexOf("\" xmlns:itunes");
      if (index > 0)
        image = ele.ToString().Substring(0, index).Replace("<itunes:image href=\"", "");
    }
Spitfire2k6
  • 308
  • 4
  • 17
0

I was looking for something similar for the <itunes:episode> tag. I know this is a rather old question, but I thought I'd add my method for someone else who might end up here. I got it working with:

var episode = item.Element("{http://www.itunes.com/dtds/podcast-1.0.dtd}episode").Value;

So for one of the samples I'd use:

var duration= item.Element("{http://www.itunes.com/dtds/podcast-1.0.dtd}duration").Value;
neildeadman
  • 3,974
  • 13
  • 42
  • 55