Search code examples
c#xmlxelement

c# Reading XML with XElement


When I run the following code and step through it with a break point and look at temp I see "Empty, Enumeration yielded no results" and the MessageBox.Show never fires. I'm trying to pull everything under Season no="1"

XElement sitemap = XElement.Load(@"http://services.tvrage.com/feeds/episode_list.php?sid=" + this.showID);
IEnumerable<XElement> temp = from el in sitemap.Elements("Season")
                                         where (string)el.Attribute("no") == "1"
                                         select el;
foreach (XElement el in temp)
{
    MessageBox.Show("found something");
}

This is the XML that's being loaded: http://services.tvrage.com/feeds/episode_list.php?sid=6312


Solution

  • You're looking for elements called Season directly under the root element... whereas your XML looks like this:

    <Show>
      <name>The X-Files</name>
      <totalseasons>9</totalseasons>
      <Episodelist>
        <Season no="1">
          <episode>
          ...
    

    If you want to look for all descendant elements with a given name, use Descendants instead of Elements. That certainly finds elements in the example XML you've given.