简体   繁体   中英

Retrieving custom element values from xml using SyndicationFeed

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?

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();
        }
    }
}

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=\"", "");
    }

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;

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM