感谢一些很好的回答,我现在了解如何使用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?
代码有一个错字,'doc'和'xdoc'变量不一样 –
固定,感谢@MrinalKamboj – octavioccl