c# xml xpath选择tsinglenode属性

本文关键字:tsinglenode 属性 选择 xpath xml | 更新日期: 2023-09-27 18:05:09

如何获取属性的值:

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

I've try

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>

c# xml xpath选择tsinglenode属性

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

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

旁注:在大多数情况下,元素索引对于查找相应的元素是不可靠的。您可能希望通过属性值进行过滤,例如,输入"查找具有属性name = X<Attr>元素": /Attr[@name='X']