Search code examples
xmlxpathxpathnavigator

read attribute value within a attribute using XPathNavigator


This is how my xPathNavigator looks like:

<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" />

I want to read the value of Path in the DisplayMemberBinding attribute.

This is what i have tried:

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

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

How do i get to the value of Path in DisplayMemberBinding attribute?


Solution

  • Use:

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

    Of course, you need to cast the result to string.

    XSLT-based verification:

    <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>
    

    When this transformation is applied on the provided XML document:

    <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" />
    

    the XPath expression is evaluated and the result of this evaluation is copied to the output:

    Id