1

I'm trying to parse a rss feed. I can get the elements out, but whatever i try, i cant get the attributes!

This is how the rss feed (xml) looks like:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>nu.nl - Algemeen</title>
    <copyright>Copyright (c) 2010, nu.nl</copyright>
    <link>http://www.nu.nl/algemeen/</link>
    <language>nl</language>
    <description>nu.nl Rich Site Summary</description>
    <pubDate>Sat, 16 Oct 2010 08:36:37 +0200</pubDate>
              <item>
        <title>Jason W. keert zich af van moslimextremisme</title>
        <link>http://www.nu.nl/binnenland/2357342/jason-w-keert-zich-af-van-moslimextremisme.html</link>
        <guid>http://www.nu.nl/binnenland/2357342/index.html</guid>
        <description>AMSTERDAM - Het vermeende lid van de Hofstadgroep Jason W. heeft het moslimextremisme naar eigen zeggen de rug toegekeerd.</description>
                <related>
            <b>Gerelateerd nieuws:</b><br />
                                    - <a href="http://www.nu.nl/binnenland/2293903/nieuw-proces-leden-hofstadgroep.html">Nieuw proces tegen leden Hofstadgroep</a><br />
                                    - <a href="http://www.nu.nl/binnenland/2175558/proces-hofstadgroep-moet.html">Proces Hofstadgroep moet over</a><br />
                                    - <a href="http://www.nu.nl/algemeen/2073580/eu-hof-verwerpt-beroep-van-lid-hofstadgroep.html">EU-hof verwerpt beroep van lid Hofstadgroep</a><br />
                    </related>
                <pubDate>Sat, 16 Oct 2010 08:36:36 +0200</pubDate>
        <category>Algemeen</category>
                <enclosure url="http://media.nu.nl/m/m1cz4nqakofe_t.jpg" type="image/jpeg" />      </item>

The attribute i'm trying to get is the 'enclosure url'.

This is my try:

private IEnumerable<Channel> getChannelQuery(XDocument xdoc)
    {

        return from channels in xdoc.Descendants("channel")
            select new Channel
            {
                Title = channels.Element("title") != null ? channels.Element("title").Value : "",
                Link = channels.Element("link") != null ? channels.Element("link").Value : "",
                Description = channels.Element("description") != null ? channels.Element("description").Value : "",
                //PubDate = channels.Element("pubDate") != null ? channels.Element("pubDate").Value : "",
                //Enclosure = channels.Element("enclosure ") != null ? channels.Element("enclosure").Value : "",
                Items = from items in channels.Descendants("item")
                    select new Item
                    {
                        Title = items.Element("title") != null ? items.Element("title").Value : "",
                        Link = items.Element("link") != null ? items.Element("link").Value : "",
                        Description = items.Element("description") != null ? items.Element("description").Value : "",
                        Guid = (items.Element("guid") != null ? items.Element("guid").Value : ""),
                        PubDate = (items.Element("pubDate") != null ? items.Element("pubDate").Value : ""),
                        Enclosure = (items.Attribute("url") != null ? items.Attribute("url").Value : "")
                    }
            };
    }

What am I doing wrong here?

1
  • how did you do that jon? Commented Oct 16, 2010 at 11:26

1 Answer 1

2

Isn't it

Enclosure = (items.Element("enclosure").Attribute("url") != null ? items.Element("enclosure").Attribute("url").Value : "")

?

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

3 Comments

yes that worked! But how? enclosure isn't an element is it? shouldn't it be an attribute?
@Yustme: I think you are confused because the enclosure is a self-closing tag, it is still an element as it starts with <
ah okay, its a while since i played with xml. thanks for clearing that up for me!

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.