【发布时间】:2017-04-03 17:57:58
【问题描述】:
感谢一些很好的答案,我现在了解如何使用 LINQ to XML 在 XML 文件中查找元素。
我正在努力解决以下问题:要找到我只知道其兄弟值的属性的值:
<books>
<book>
<author>Douglas Adams</author>
<title>The Hitch Hikers Guide to the Galaxy</title>
<price>42</price>
<locationId>1</locationId>
<quantity>0</quantity>
</book>
<book>
<author>Douglas Adams</author>
<title>The Hitch Hikers Guide to the Galaxy</title>
<price>42</price>
<locationId>2</locationId>
<quantity>7</quantity>
</book>
<book>
<author>Douglas Adams</author>
<title>The Hitch Hikers Guide to the Galaxy</title>
<price>42</price>
<locationId>3</locationId>
<quantity>20</quantity>
</book>
<book>
<author>Douglas Adams</author>
<title>The Hitch Hikers Guide to the Galaxy</title>
<price>42</price>
<locationId>4</locationId>
<quantity>5</quantity>
</book>
</books>
如果我只知道位置 ID,我如何找到这本书的数量?假设我想要quantity 换成locationId = 3。
我的方法是创建一个循环并在找到所需的位置 ID 后立即停止。这听起来像是最好的方法吗?有没有更简单的方法可以使用 LINQ to XML 完成此任务?
【问题讨论】:
标签: c# xml linq linq-to-xml