我是XDocument和LINQ的新手。这里是我想要做的事:LINQ to XML查询中的基本过滤
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<root>
<chapters total-chapters="3">
<Chapter chapter-no="1">
<chapter-summary>this is chapter 1</chapter-summary>
</Chapter>
<Chapter chapter-no="2">
<chapter-summary>this is chapter 2</chapter-summary>
</Chapter>
<Chapter chapter-no="3">
<chapter-summary>this is chapter 3</chapter-summary>
</Chapter>
<Chapter chapter-no="4">
<chapter-summary>this is chapter 4</chapter-summary>
</Chapter>
</chapters>
</root>
现在我需要阅读所有与特定章节,没有记录。我写我的LINQ查询为:
IEnumerable<XElement> elem_list =
from e in xdoc.Elements("Chapter")
where (string) e.Attribute("chapter-no") == "1"
select e;
foreach (XElement e in elem_list)
{
Console.WriteLine(e);
}
但是elem_list没有得到填充,也没有显示任何内容。
XPath的似乎是非常有帮助...谢谢 – 2013-03-17 03:20:57