سؤال

I have a collection of Silverlight 5 XAML documents that start with the following structure

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"  d:DesignWidth="1000" d:DesignHeight="800" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
   <Grid x:Name="LayoutRoot">
      <Grid.RowDefinitions>
         <RowDefinition Height="*" />
         <RowDefinition Height="*" />
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
         <ColumnDefinition Width="280"></ColumnDefinition>
         <ColumnDefinition Width="50*"></ColumnDefinition>
         <ColumnDefinition Width="50*"></ColumnDefinition>
      </Grid.ColumnDefinitions>
      ...other content below

I need an XPath expression that allows me to select the RowDefinition and ColumnDefinition elements.

I have tried:

XmlDocument xdoc = new XmlDocument();
XmlNamespaceManager mgr = new XmlNamespaceManager(xdoc.NameTable);          
xdoc.LoadXml(xaml);
XmlNodeList rowDefinitions = xdoc.DocumentElement.ChildNodes[0].SelectNodes("//RowDefinition", mgr);

But not had any success. What do I need to do?

هل كانت مفيدة؟

المحلول

Figured it out, posting answer here in case anyone has the same issues.

The key is that you have to specify the namespace of the element you're parsing, even if the XAML does not have the namespace for that element explicitly declared. In my case, adding:

mgr.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");

And then referencing the RowDefinition elements as:

xdoc.SelectNodes("//x:RowDefinition", mgr)

Was able to pull it off.

نصائح أخرى

Why don't you define the structure inside the XAML like this

<Border Grid.Column="1" Grid.Row="1" BorderThickness="3" BorderBrush="Black" CornerRadius="10" Height="AUTO" Background="DarkGray">

in this case its for border but can be use for any tag,it becames simpler if want to define how the content gona be divided in the grid in the XAML side instead of the C#.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top