2017-04-03 85 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