0

I'm trying to extract data from a wordpress blog xml feed, this is the format of the xml: http://brockallen.com/category/asp-net/feed/

var xElements = XElement.Parse(ResponseText);
var blogs = (from temp in xElements.Elements()
             select new 
             {
                 Title = temp.Element("item").Element("title").Value,
                 URL = temp.Element("item").Element("link").Value,
                 Image = temp.Element("item").Element("media:content").Value
             }).FirstOrDefault();

How do I get them all, and store them in to an object?

Error on media:content is saying The ':' character, hexadecimal value 0x3A, cannot be included in a name.`

When I remove that line, it only gets one, when there are more, even after removing FirstOrDefault()

5
  • 1
    You're explicitly calling FirstOrDefault. If you don't just want the first item, don't do that :) Commented Sep 22, 2014 at 15:19
  • @JonSkeet Hmm, I took that out and it works, but still only returns 1, when there's more Commented Sep 22, 2014 at 15:21
  • The blogs variable has only [0] = 1 result in Results View Commented Sep 22, 2014 at 15:23
  • @GeorgePolevoy please review brockallen.com/category/asp-net/feed for an example Commented Sep 22, 2014 at 15:27
  • updated the answer with a sample of similar structure Commented Sep 22, 2014 at 15:39

2 Answers 2

2

Your are working on the wrong level of the document. Also you need an namespace.

  XNamespace media = "http://search.yahoo.com/mrss/";
  var items =
  from channelElement in XElement.Parse(ResponseText).Element("channel").Elements("item")
  select new {
    Title = channelElement.Element("title").Value,
    Url = channelElement.Element("link").Value,
    MediaItems = (
        from mediaItemElement in channelElement.Elements(media + "content")
        select new {
            url = mediaItemElement.Attribute("url").Value,
            medium = mediaItemElement.Attribute("medium").Value
            }
        ).ToList()
  };
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, this works great. Thanks.. Except its throwing an error when I try to get media:content node: The ':' character, hexadecimal value 0x3A, cannot be included in a name.
@ZeeTee: That's because it's using a namespace with an alias of media. Look up using namespaces in LINQ to XML.
Is there a way to easily plug it in your code or is it a larger task than your code
updated the answer so you are able to show media:content as a list
1

Try this...updated with namespace for querying content element

    // var xDoc = XDocument.Load(@"<your xml location>");
    var xDoc = XDocument.Parse("<your xml string>");
    XNamespace ns = "http://search.yahoo.com/mrss/";

    var blogs = (from temp in xDoc.Descendants().Elements("item")
                 select new
                 {
                     Title = temp.Element("title").Value,
                     URL = temp.Element("link").Value,
                     Image = temp.Element(ns + "content").Value
                 });

2 Comments

this way you can only get the first media item for each title, while there are a lot of them in your document
to get all content elements, just use Image = temp.Elements(ns + "content") or if only image urls are needed then - Image = temp.Elements(ns + "content").Attributes("url")

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.