如何从作为字符串提供的xpath中获取最后一个元素名称

时间:2014-11-24 14:33:20

标签: c# xpath

我在visual studio中使用C#,尝试从第三方库提供的xpath格式的字符串中提取元素名称。

var xPath = "/TransactionValues/Entities/Entity [Roles/Role='FinancialInstitutionSubstituteCheckContact']/Locations/Location [@Use='Primary']/( normalize-space( concat( PrimaryAddressLine, ' ', SecondaryAddressLine, ', ',City, ', ', State, '  ', PostalCode ) ) )"

我想要的元素名称是"位置"从上面的字符串。我们当前的方法失败是因为字符串末尾的[@Use='Primary']/(...部分,在最后一个元素名称之后有一个额外的正斜杠,它会抛出我们当前的方法。

当前方法

xPath.Remove(0, xPath.LastIndexOf("/") + 1);

这会返回"( normalize-space( concat( PrimaryAddressLine, ' ', SecondaryAddressLine, ', ',City, ', ', State, ' ', PostalCode ) ) )"

我过去半小时一直在试图查看是否有一个 C#类代表XPath作为字符串,这样我就可以轻松提取也许是最后一个节点名称,但我发现的只有System.Xml.XPath.XPathNavigator类似乎需要一个完整的xml文档才能创建一个System.Xml.XPath.XPathExpression我可以使用它,但我不是&# 39; t有一个完整的xml文档,我只有上面的字符串。

修改

我们正在尝试使用第三方提供的XPath将用户对提示的响应注入xml文件。 XPath应该指定注入响应的位置。我不明白XPath的 end 部分( normalize-space( concat( PrimaryAddressLine, ' ', SecondaryAddressLine, ', ',City, ', ', State, ' ', PostalCode ) ) )试图说什么。

Xml的结构与此类似:

<TransactionValues>
  <Entities>
    <Entity id="Financial_Institution_Substitute_Check_Contact">
      <Locations>
        <Location>
          <City>City</City>
          <PostalCode>Zip</PostalCode>
          <PrimaryAddressLine>Addy1</PrimaryAddressLine>
          <SecondaryAddressLine>Addy2</SecondaryAddressLine>
          <State>State</State>
        </Location>
      </Locations>
      <NonIndividual>
        <Contact>
          <AdditionalInformation>
            <Description>address-phone of bank</Description>
          </AdditionalInformation>
        </Contact>
      </NonIndividual>
      <Roles>
        <Role>FinancialInstitutionSubstituteCheckContact</Role>
      </Roles>
    </Entity>
  </Entities>
</TransactionValues>

0 个答案:

没有答案