我需要在 Asp.Net 应用程序中解析读取的 XAML 文档并解析它并在循环时检索每列的转换器值。
这就是我的 XML (XAML) 的样子:
<Presentation>
<Columns>
<mux:ColumnCollection xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:mux="http://schemas.microsoft.com/SystemCenter/Common/UI/Views/GridView" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:datebinding="clr-namespace:Microsoft.EnterpriseManagement.UI.Extensions;assembly=Microsoft.EnterpriseManagement.UI.Extensions" xmlns:data="clr-namespace:Microsoft.EnterpriseManagement.UI.Extensions;assembly=Microsoft.EnterpriseManagement.UI.Extensions" xmlns:appCommon="clr-namespace:Microsoft.EnterpriseManagement.ServiceManager.Application.Common;assembly=Microsoft.EnterpriseManagement.ServiceManager.Application.Common" xmlns:toolbox="clr-namespace:Microsoft.EnterpriseManagement.UI.WpfToolbox;assembly=Microsoft.EnterpriseManagement.UI.FormsInfra" xmlns:slaBinding="clr-namespace:Microsoft.EnterpriseManagement.ServiceManager.SLA.Common;assembly=Microsoft.EnterpriseManagement.ServiceManager.SLA.Common" xmlns:viewtools="clr-namespace:SomeLibrary.ViewTools;assembly=SomeLibrary.ViewTools">
<mux:Column Name="Col1" DisplayMemberBinding="{Binding Path=Prop1, Mode=OneWay}" Width="100" DisplayName="Header_Prop1" Property="Porp1" DataType="s:String" />
<mux:Column Name="Col2" DisplayMemberBinding="{Binding Path=Prop2, Mode=OneWay}" Width="200" DisplayName="Header_Prop2" Property="Prop2" DataType="s:String" />
<mux:Column Name="Col3" DisplayMemberBinding="{Binding Path=Prop3, Mode=OneWay}" Width="200" DisplayName="Header_Prop3" Property="Prop3" DataType="s:String" />
<mux:Column Name="Col4" DisplayMemberBinding="{Binding Path=Prop4, Mode=OneWay, Converter={x:Static viewtools:Converter1.Default}}" Width="160" DisplayName="Prop4" Property="Prop4" DataType="s:DateTime" />
<mux:Column Name="Col5" Width="300" DisplayName="Prop5" Property="Prop4" DataType="s:String">
<mux:Column.CellTemplate>
<DataTemplate>
<TextBlock Height="Auto" Width="Auto" TextWrapping="Wrap" >
<TextBlock.Text>
<MultiBinding Converter="{x:Static viewtools:Converter2.Default}">
<MultiBinding.Bindings>
<Binding Path="SomeObject1" />
<Binding Path="SomeObject2" />
</MultiBinding.Bindings>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</mux:Column.CellTemplate>
</mux:Column>
<mux:Column Name="COl6" Width="75" DisplayName="Prop6" Property="Prop6" DataType="s:String">
<mux:Column.CellTemplate>
<DataTemplate>
<TextBlock Height="Auto" Width="100" Text="{Binding Path=SomeObject3, Mode=OneWay, Converter={x:Static viewtools:Converter2.Default}}" Background="{Binding Path=SomeObject3, Converter={x:Static viewtools:Converter4.Default}}" TextAlignment="Center" VerticalAlignment="Center" Margin="0" HorizontalAlignment="Center"/>
</DataTemplate>
</mux:Column.CellTemplate>
</mux:Column>
</mux:ColumnCollection>
</Columns>
</Presentation>
这是我正在使用的代码:
XmlNodeList columnNodes = configXML.GetElementsByTagName("mux:ColumnCollection");
foreach (XmlNode col in columnNodes[0].ChildNodes.Cast<XmlNode>().Where(n=>n.NodeType != XmlNodeType.Comment))
{
if (col.NodeType != XmlNodeType.Comment)
{
string converterName = string.Empty;
string methodName = string.Empty;
if (col.Attributes.GetNamedItem("DisplayMemberBinding").Value.Contains("Converter"))
{
string displayMemberBinding = col.Attributes.GetNamedItem("DisplayMemberBinding").Value;
string converterInstanceName = displayMemberBinding.Substring(displayMemberBinding.LastIndexOf(":") + 1, displayMemberBinding.IndexOf("}") - displayMemberBinding.LastIndexOf(":"));
converterName = converterInstanceName.Substring(0, converterName.IndexOf("."));
methodName = converterInstanceName.Substring(converterName.IndexOf(".") + 1);
}
if (col.HasChildNodes)
{
converterName = col.LastChild.ChildNodes[0].LastChild.ChildNodes[0].FirstChild.Attributes.GetNamedItem("Converter").Value;
}
}
}
代码的问题在于它使用了大量的字符串操作,并且还需要根据转换器的定义位置和附加的属性来进行大量自定义条件。
是否有更好的(通用)方法来解析和操作此 XML (XAML) 文档并获取转换器名称和方法名称以及它将修改的属性?
编辑:我也尝试使用 XamlReader 并添加了对 PresentationCore dll 的引用,但我在 System.Windows.Markup 命名空间中看不到 XamlReader 类。