使用XPathNavigator读取属性中的属性值

时间:2013-05-01 16:59:13

标签: xml xpath xpathnavigator

这就是我的xPathNavigator的样子:

<mux:Column Name="id" DisplayMemberBinding="{Binding Path=Id, Mode=OneWay}" Width="100" DisplayName="Header_Id" Property="Id" DataType="s:String" xmlns:mux="http://schemas.microsoft.com/SystemCenter/Common/UI/Views/GridView" />

我想在DisplayMemberBinding属性中读取Path的值。

这就是我的尝试:

xPathNavigator.GetAttribute("DisplayMemberBinding", "") //Gives me {Binding Path=Id, Mode=OneWay}

xPathNavigator.GetAttribute("DisplayMemberBinding/Binding/@Path", "") //Gives me empty string

如何在DisplayMemberBinding属性中获取Path的值?

1 个答案:

答案 0 :(得分:1)

使用

xPathNavigator.Evaluate
  (@"substring-before(substring-after(@DisplayMemberBinding, 'Path='),
                     ',')"
  );

当然,您需要将结果转换为string

基于XSLT的验证

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:mux="http://schemas.microsoft.com/SystemCenter/Common/UI/Views/GridView">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <xsl:value-of select=
  "substring-before(substring-after(@DisplayMemberBinding, 'Path='),
                    ',')"/>
 </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用此转换时:

<mux:Column Name="id"
DisplayMemberBinding="{Binding Path=Id, Mode=OneWay}"
Width="100" DisplayName="Header_Id"
Property="Id" DataType="s:String"
xmlns:mux="http://schemas.microsoft.com/SystemCenter/Common/UI/Views/GridView" />

评估XPath表达式并将此评估结果复制到输出中:

Id