c#xml xpath SelectSingleNode属性

时间:2015-07-28 09:41:32

标签: c# xml xpath

如何获取属性的值:

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

我试过

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

以下适用于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>

1 个答案:

答案 0 :(得分:0)

假设上下文节点为<Setup>,则第六个value下第五个<Att>获取<Group>属性的正确XPath如下:

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

旁注:在大多数情况下,元素索引对于查找相应元素并不可靠。您可能希望按属性值进行过滤,例如,通过说“找到<Attr>元素,其属性name等于X/Attr[@name='X']