简体   繁体   中英

c# xml xpath SelectSingleNode attribute

How can I get the value of attribute:

/Document/Setup/Info/Att/Group[6]/Attr[5]@value

I've tried

String S = nest.SelectSingleNode("./osis:Info/osis:Att/osis:Group[6]>osis:Attr[5]@value).Value;

The following works for the FilePath :

String F = nest.SelectSingleNode("./osis:Info/osis:FilePath", >xmlns).Value;

XML:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="http://www.ns.com/ns/ns">
  <Setup>
    <Info>
      <FilePath>\\computer1\project\E2002307\E2002307.drg</FilePath>
      <Att>
        <Group class="custom" name="Manu" desc="attributes"
            ord="6">
          <Attr num="119" name="xyz" desc="zyx" type="s" ord="1" value="S355">
            <Valid perm="e" max="100"/>
          </Attr>
          <Attr num="120" name="thick" desc="thick." type="r" ord="2" value="5">
            <Valid perm="e" min="0" max="99999"/>
          </Attr>
          <Attr num="121" name="units" desc="units." type="s" ord="4" value="mm">
            <Valid perm="e" expr="mm" max="80"/>
          </Attr>
          <Attr num="123" name="time" desc="minutes." type="r" ord="24">
            <Valid perm="e" min="0"/>
          </Attr>
          <Attr num="124" name="X" desc="X direction." type="r" ord="11" value="3">
            <Valid perm="e" min="0"/>
          </Attr>
          <Attr num="125" name="Y" desc="Y direction." type="r" ord="12" value="1">
            <Valid perm="e" min="0"/>
          </Attr>
        </Group>

Assuming that the context node is <Setup> , then the correct XPath to get value attribute of the fifth <Att> under the sixth <Group> would be as follow :

./osis:Info/osis:Att/osis:Group[6]/osis:Attr[5]/@value

Side note: In most case, element indices aren't reliable for finding the corresponding element. You may want to filter by attributes value instead, fe by saying "find <Attr> element having attribute name equals X " : /Attr[@name='X']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM