基于节点值的LINQ-to-XML选择,新手问题

时间:2010-03-08 20:29:21

标签: vb.net linq-to-xml

鉴于以下XML,我想返回eventtitles id = 23的所有eventtype。我当前的查询仅查看第一个eventtype,因此返回错误的结果。

    <event>
      <eventtitle>Garrison Keillor</eventtitle>
      <eventtypes>
        <eventtype id="24"/>
        <eventtype id="23"/>
      </eventtypes>
    </event>
    <event>
      <eventtitle>Joe Krown Trio featuring Walter Wolfman Washington</eventtitle>
      <eventtypes>
        <eventtype id="23"/>
      </eventtypes>
    </event>

LINQ查询:

Dim query = _
From c In calXML...<event> _
Where c...<eventtypes>.<eventtype>.@id = "23" _
Select c.<eventtitle>.Value, c.<eventlocation>.Value


For Each item In query
    Response.Write("<h3>" & item.eventtitle & "</h3>")
    Response.Write(item.eventlocation & "<br />")
Next

1 个答案:

答案 0 :(得分:1)

您需要拨打Any,如下所示:

Dim query = _ 
From c In calXML...<event> _ 
Where c.<eventtypes>.<eventtype>.Any(Function(t) t.@id = "23") _ 
Select c.<eventtitle>.Value, c.<eventlocation>.Value