2017-04-03 84 views
0

感謝一些很好的回答,我現在瞭解如何使用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只?假設我希望quantitylocationId = 3

我的方法是創建一個循環,並在找到所需的位置ID後立即停止。這聽起來像是最好的方法嗎?有沒有更簡單的方法來完成這個使用LINQ to XML?

回答

2

使用Descendants方法獲取所有書籍,然後您可以使用Where擴展方法和項目使用Select分機過濾。方法quantity

XDocument xdoc = XDocument.Load(path); 
var result=xdoc.Descendants("book") 
       .Where(e=>(int)e.Element("locationId")==3) 
       .Select(e=>(int)e.Element("quantity")) 
       .FirstOrDefault(); 
+1

代碼有一個錯字,'doc'和'xdoc'變量不一樣 –

+0

固定,感謝@MrinalKamboj – octavioccl

1

首先,你必須與結束標記問題locationId

LINQ到XML有一個NodesAfterSelf方法,因此,如果標籤的oreder始終是相同的,你可以使用:

var quantityElement = xdoc.Descendants("locationId") 
      .First(l=>l.Value=="3") 
      .NodesAfterSelf() 
      .FirstOrDefault(); 

編輯 -

其實上面可以拋出一個異常,如果沒有找到位置。以下不會。

var quantityElement = xdoc 
      .Descendants("locationId") 
      .Where(l=>l.Value=="3") 
      .SelectMany(l=>l.NodesAfterSelf()) 
      .FirstOrDefault(); 
+0

感謝@Ofir。這只是關於結束標籤的錯字。現在會解決它。 – jrn

1

對於VB'ers可能需要此功能。以下解決方案不會對標籤順序做出假設。

Dim xe As XElement 
    'for testing 
    xe = <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> 

    'the answer - by selecting the <book> all nodes are available. 
    Dim aBook As XElement = xe...<book>.SingleOrDefault(Function(el) el.<locationId>.Value = "3") 

    'verification 
    If aBook IsNot Nothing Then 
     Debug.WriteLine(aBook.<quantity>.Value) 
     Debug.WriteLine(aBook.<author>.Value) 
     Debug.WriteLine(aBook.<title>.Value) 
    End If