Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a node with attributes from SelectSingleNode

Tags:

c#

xml

I am quite the n00b but lately I have been playing with parsing some XML data. I actually found a nice feature on this site where I can get to a specific node with a specific attribute by doing: docFoo.SelectSingleNode("foo/bar/baz[@name='qux']); However, the data looks like this:

<saving-throws>
    <saving-throw>
        <name>Fortitude</name>
        <abbr>Fort</abbr>
        <ability>Con</ability>
        <modifiers>
            <modifier name="base" value="2"/>
            <modifier name="ability" value="5"/>
            <modifier name="magic" value="0"/>
            <modifier name="feat" value="0"/>
            <modifier name="race" value="0"/>
            <modifier name="familar" value="0"/>
            <modifier name="feature" value="0"/>
            <modifier name="user" value="0"/>
            <modifier name="misc" value="0"/>
        </modifiers>
    </saving-throw>
    <saving-throw>
        <name>Reflex</name>
        <abbr>Ref</abbr>
        <ability>Dex</ability>
        <modifiers>
            <modifier name="base" value="6"/>
            <modifier name="ability" value="1"/>
            <modifier name="magic" value="0"/>
            <modifier name="feat" value="0"/>
            <modifier name="race" value="0"/>
            <modifier name="familar" value="0"/>
            <modifier name="feature" value="0"/>
            <modifier name="user" value="0"/>
            <modifier name="misc" value="0"/>
        </modifiers>
    </saving-throw>

And I want to be able to get the node with name=base but for each saving-throw node where childnode "abbr" = xx. Can I somehow do that in a single SelectSingleNode or am I going to have to stop at saving throw and walk through the rest of the tree?

like image 393
bdefreese Avatar asked Jan 22 '23 00:01

bdefreese


1 Answers

This should give you exactly what you want:

SelectSingleNode("/saving-throws/saving-throw[abbr = 'Fort']/modifiers/modifier[@name='base']");
like image 167
tpeczek Avatar answered Jan 25 '23 23:01

tpeczek