繁体   English   中英

c#使用XElement读取XML

[英]c# Reading XML with XElement

当我运行以下代码并在断点处逐步执行并查看temp时,我看到“空,枚举未产生任何结果”,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中找到元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM