Getting a node with attributes from SelectSingleNode
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?
This should give you exactly what you want:
SelectSingleNode("/saving-throws/saving-throw[abbr = 'Fort']/modifiers/modifier[@name='base']");
Try this, this will give you the modifier tag who's saving-throw ancestor's child abbr tag is "Fort"
//saving-throw/modifiers/modifier[@name='base' and ../../abbr = "Fort"]
精彩评论