【发布时间】:2012-07-07 17:23:44
【问题描述】:
我有一个 xml 文件。
<BOOK bnumber="1" bname="Book">
<CHP cnumber="1">
<VER vnumber="1">This is the sentence 1.</VER>
<VER vnumber="2">This is the sentence 2.</VER>
<VER vnumber="3">This is the sentence 3.</VER>
</CHP>
<CHP cnumber="2">
<VER vnumber="1">Hello World 1.</VER>
<VER vnumber="2">Hello World 2.</VER>
<VER vnumber="3">Hello World 3.</VER>
<VER vnumber="4">Hello World 4.</VER>
</CHP>
<!--MANY: Thousand records-->
</BOOK>
我想获取属性“cnumber”。结果:
Chapter={"CHP 1";"CHP 2",....};
我未完成的代码:
XDocument xdoc = XDocument.Load("Book.xml");
var temp = xdoc.Descendants("CHP").Where(x => x.Attribute("cnumber").Value != "0");
谢谢。
【问题讨论】:
-
什么不起作用?我不完全理解你的期望是什么。
-
顺便说一下,您的示例数据格式不正确。您以
<VER>开始标签并以</Sentence>结束标签 -
刚刚更正了。谢谢提醒。
标签: c# xml c#-4.0 xml-parsing linq-to-xml