Xml 元素未能选择正确的子节点

Xml 元素未能选择正确的子节点,xml,vb.net,linq,Xml,Vb.net,Linq,我有以下XML: <inventory> <item> <stocktype> <type>Basket</type> <id>1</id> <parentId>0</parentId> </stocktype> <cost>10.00</cost> <quantity>

我有以下XML:

<inventory>
  <item>
    <stocktype>
      <type>Basket</type>
      <id>1</id>
      <parentId>0</parentId>
    </stocktype>
    <cost>10.00</cost>
    <quantity>1</quantity>
    <name>Golden Wick Basket</name>
    <note>
      <code>remark</code>
      <content>Be extra careful about the golden paint....</content> 
    </note> 
    <note>
      <code>usage</code>
      <content>DeluxFruitBasket</content> 
    </note>  
  </item>
  <item>
    <stocktype>
      <type>Fruit</type>
      <id>2</id>
      <parentId>1</parentId>
    </stocktype>
    <cost>6.00</cost>
    <quantity>10</quantity>
    <name>Apple</name>
    <note>
      <code>remark</code>
      <content>Please pick red apples only</content> 
    </note>
    <note>
      <code>usage</code>
      <content>DeluxFruitBasket</content> 
    </note>
  </item>
  <item>
    <stocktype>
      <type>Fruit</type>
      <id>3</id>
      <parentId>1</parentId>
    </stocktype>
    <cost>4.00</cost>
    <quantity>10</quantity>
    <name>Orange</name>
    <note>
      <code>remark</code>
      <content></content> 
    </note>
    <note>
      <code>usage</code>
      <content>DeluxFruitBasket</content> 
    </note>
  </item>
  <item>
    <stocktype>
      <type>Fruit</type>
      <id>4</id>
      <parentId>1</parentId>
    </stocktype>
    <cost>12.00</cost>
    <quantity>1</quantity>
    <name>Pineapple</name>
  </item>
</inventory>
我尝试过此LINQ to XML查询,但其计算结果始终为false:

If(_rootElement.Descendants("item").Any(Function (x) x.Element("stocktype").Element("type").Value = "Fruit" And x.Element("note").Element("content").Value = "DeluxFruitBasket")) 
我使用linqpad检查并用Where()替换Any(),查询返回null

问题:

  • 为什么查询返回null
  • 我还发现x.Element(“note”).Element(“content”)总是从这两个元素中选择第一个“note”元素,为什么
  • 我不能改变XML的结构,因为它是我公司的标准,如何重写查询以归档我的意图
多谢各位

“为什么查询返回空值?”

因为XML中的
item
元素都没有第一个
内容
等于
“DeluxFruitBasket”
(为什么它必须是
item
中的第一个
内容
与问题2有关)

“我还发现,
x.Element(“note”).Element(“content”)
总是从两个元素中选择第一个“note”元素,为什么?”

因为您使用了
Element()
而不是复数
Elements()
。后者将返回您想要的所有匹配元素

这是一种可能的LINQ,用于获取包含“水果”
stocktype
的所有
项目以及至少一个“DeluxFruitBasket”
内容的
(将
其中的
替换为
任何
,以便在代码中使用此项):

“为什么查询返回空值?”

因为XML中的
item
元素都没有第一个
内容
等于
“DeluxFruitBasket”
(为什么它必须是
item
中的第一个
内容
与问题2有关)

“我还发现,
x.Element(“note”).Element(“content”)
总是从两个元素中选择第一个“note”元素,为什么?”

因为您使用了
Element()
而不是复数
Elements()
。后者将返回您想要的所有匹配元素

这是一种可能的LINQ,用于获取包含“水果”
stocktype
的所有
项目以及至少一个“DeluxFruitBasket”
内容的
(将
其中的
替换为
任何
,以便在代码中使用此项):


谢谢,再看一遍MSDN文档,我才意识到它确实在页面顶部写了第一个(按文档顺序)。我总是跳过页面的那一部分。怪不得我错过了。和linqpad核对过了,效果很好。非常感谢你!谢谢,只要再读一遍MSDN文档,我就意识到它确实在页面顶部写了第一个(按文档顺序)。我总是跳过页面的那一部分。怪不得我错过了。和linqpad核对过了,效果很好。非常感谢你!
Dim result = doc.Root.
                 Descendants("item").
                 Where(Function(x) x.Element("stocktype").
                                     Element("type").Value = "Fruit" _
                                     AndAlso _
                                   x.Elements("note").
                                     Elements("content").
                                     Any(Function(c) c.Value = "DeluxFruitBasket"))