感謝一些很好的回答,我現在瞭解如何使用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