0

I have the following xml

    <?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<entry><id><![CDATA[text]]></id><
author><name><![CDATA[film24]]></name></author><title><![CDATA[text]]></title>
<updated>2009-10-30T15:55:13+00:00</updated><published>2009-10-30T00:00:00+00:00</published>
<media:thumbnail type="image/jpeg" title="thumbnail" url=""/>
<media:content type="video/flv" title="video" url="" expression="high"/>
<media:content type="video/flv" title="video" url="" expression="low"/>
</entry>
</feed>

If i include the following namespace i cannot query the xml. ie it doesn't return any results.

http://www.w3.org/2005/Atom

Here is how i query the xml

        XNamespace nsMedia = "http://search.yahoo.com/mrss/";
        XNamespace nsAtom = "http://www.w3.org/2005/Atom";

        string url = HttpContext.Current.Server.MapPath(ConfigHelper.GetValue("FeedUri"));
        var feed = XElement.Load(url);
        var posts = from c in feed.Descendants(nsAtom + "entry")
                    select new CDNEntry
                    {
                         Id = (string)c.Element(nsAtom + "id").Value,
                         Author = (string)c.Element(nsAtom + "author").Value,
                         Title = (c.Element("title") != null) ? (string)c.Element(nsAtom + "title").Value : "",
                         Summary = (c.Element("summary") != null) ? (string)c.Element(nsAtom + "summary").Value : "",
                         Thumbnail = (string)c.Element(nsMedia + "thumbnail").Attribute(nsAtom + "url").Value,
                         FLV = (string)c.Element(nsMedia + "content").Attribute(nsAtom + "url").Value,
                         Updated = DateTime.Parse((string)c.Element(nsAtom + "updated").Value),
                         Published = DateTime.Parse((string)c.Element(nsAtom + "published").Value)
                    };

        return posts.ToList();
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
frosty
  • 5,330
  • 18
  • 85
  • 122
  • Have you verified that uniqueUrl matches at least one entries "id" element exactly? As a first step, I'd recommend temporarily removing your where lambda to isolate whether the problem is matching an id or an issue with the structure of your linq statement. – Ryan Brunner Oct 29 '09 at 13:05
  • hi ryan, yeah i've tried that. To clarify it doesn't work when "xmlns="http://www.w3.org/2005/Atom" is included. – frosty Oct 29 '09 at 15:00

1 Answers1

2

Your sample XML got cut off, but your problem is most likely that you need to use more namespaces. Try something like this:

XNamespace nsMedia = "http://search.yahoo.com/mrss/";
XNamespace nsAtom = "http://www.w3.org/2005/Atom";

var feed = GetFeed();
var posts = from c in feed.Descendants(nsAtom + "entry")
            where (string)c.Element(nsAtom + "id") == uniqueUrl
            select new CDNEntry
            {
                Id = (string)c.Element(nsAtom + "id"),
                Author = (string)c.Element(nsAtom + "author"),
                Title = (string)c.Element(nsAtom + "title") ?? "",
                Summary = (string)c.Element(nsAtom + "summary") ?? "",
                Thumbnail = (string)c.Element(nsMedia + "thumbnail").Attribute("url"),
                FLV = (string)c.Element(nsMedia + "content").Attribute("url"),
                Updated = (DateTime)c.Element(nsAtom + "updated"),
                Published = (DateTime)c.Element(nsAtom + "published")
            };
dahlbyk
  • 75,175
  • 8
  • 100
  • 122
  • thanks, really appreciate your response. I'm sure your right. Tho i just can't seem to get the name space correct. I've update my above code to display all xml – frosty Nov 02 '09 at 18:23
  • You're really close, you just need to remove the namespaces from Attribute("url"). Rather than use the Value property, which can cause null reference exceptions like you're seeing, I prefer to take advantage of the various explicit casts that XElement provides. Casting to string just returns Value anyway, but it just returns null if the element doesn't exist. Similarly, casting to DateTime (or DateTime? to support nulls) is cleaner than parsing the string. – dahlbyk Nov 03 '09 at 04:53
  • cool, right you are. Didn't know about the ?? operator. It's a lot cleaner. thanks. – frosty Nov 13 '09 at 13:17