开发者

Linq To Xml - Unexpected search results

Just when I was thinking that I had Linq To Xml sussed I'm faced with yet another开发者_StackOverflow中文版 error! I think if I was to understand the linq search process in general better I might have more success, so any good links regarding that are also welcome. To my problem however; using the code below:

    Dim xd As XDocument = _
    <?xml version="1.0" encoding="utf-8"?>
    <root>
        <element>
            <subelement id="1"/>
            <subelement id="2"/>
            <subelement id="3"/>
        </element>
        <element>
            <subelement id="4"/>
            <subelement id="1"/>
            <subelement id="5"/>
        </element>
    </root>

    Dim results = _
    From q In xd.Descendants.<element> _
    Where q.<subelement>.@id = 1

    For Each xe As XElement In results
        Console.WriteLine(xe.ToString)
    Next

I would have expected the above code to return both 'element' nodes, but it only returns the first because it only searches the first 'subelement' node within 'element', how can I make the where clause apply to all 'subelement' nodes?


You could do

Dim results = _
    From q In xd.Descendants.<element> _
    From p In q.<subelement> _
    Where p.@id = 1

(That is, if my VB.NET is up to the task here... I could do it in C#. Please feel free to edit.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜