none
Finding element in xaml with xpath

    Question

  • This is how i do it now:

    XmlDocument InXamlDoc = new XmlDocument();

    InXamlDoc.Load(Xml_Reader);

    XmlElement xamlRoot = InXamlDoc.DocumentElement;//DOM xaml file

    xamlNodeAttrName = xamlRoot.SelectSingleNode("/Grid/CheckBox[1]/@Name");

    and xaml file:

    <Grid
     xmlns="
    http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
      <ListView Margin="181,27,11,102" Name="lvItemMetadata" IsSynchronizedWithCurrentItem="True"/>
      <ListBox HorizontalAlignment="Left" Margin="23,28,0,102" Width="146" Name="lbCheckInItems" IsSynchronizedWithCurrentItem="True"/>
      <CheckBox HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="23,0,0,72" Width="227" Height="17" Name="chkCheckInKmap" Content="Check In the K-map"/>
      <CheckBox HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="23,0,0,51" Width="227" Height="17" Name="chkDeleteOfflineCopies" Content="Delete offline copies"/>
      <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Margin="25,7,0,0" Width="165" Height="20" Name="tbCheckInDoc" Text="Documents to check in" TextWrapping="Wrap"/>
      <TextBlock VerticalAlignment="Top" Margin="179,7,176,0" Height="15" Name="tbItemMetadata" Text="Metadata" TextWrapping="Wrap" />
      <DockPanel Name="BtnSelectAll" Height="19" Width="71" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="25,0,0,29">
        <DockPanel.Background>
          <ImageBrush
       ImageSource="DialogButton.gif" >
          </ImageBrush>
        </DockPanel.Background>
        <TextBlock Name="txtBtnSelectAll" Margin="0,-4,0,0" FontSize="10" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" Height="10.9075">Select all</TextBlock>
      </DockPanel>
    </Grid>

    since xaml have namespaces i cant get the xpath to find the right element, if i remove the namespaces it works fine. How can i use xpath and still have namespaces?

    Thursday, December 07, 2006 9:04 AM

Answers

  • Hi, you need a namespace manager to do that. The following is a sample code that shows you how to create a namespace manager and use it.

    XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
    manager.AddNamespace(
    "xaml", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
    manager.AddNamespace(
    "x", "http://schemas.microsoft.com/winfx/2006/xaml");

    XmlNodeList nodes = xml.SelectNodes("//xaml:TextBlock", manager);

    Thursday, December 07, 2006 10:34 AM