c# 使用 XElement 读取 XML

c# Reading XML with XElement

当我 运行 以下代码并使用断点单步执行它并查看 temp 时,我看到 "Empty, Enumeration yielded no results" 并且 MessageBox.Show 永远不会触发。我正在尝试将所有内容都放在第 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");
}

这是正在加载的 XML: http://services.tvrage.com/feeds/episode_list.php?sid=6312

您正在根元素下直接 寻找名为 Season 的元素...而您的 XML 看起来像这样:

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

如果要查找具有给定名称的所有后代元素,请使用 Descendants 而不是 Elements。这肯定会在您给出的示例 XML 中找到元素。