Asdfg
Asdfg

Reputation: 12253

parse XML (XAML) in asp.net web application

I need to parse read XAML document in Asp.Net application and parse it and retrieve Converter values for each column while looping thru.

This is how my XML (XAML) looks like:

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

This is the code i am using:

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

    }
}

Problem with the code is that it is using a lot of string manipulation and also requires a lot of custom conditions based on where the converter is defined and what property it is attached to.

Is there a better (generic) way to parse and manipulate this XML (XAML) document and get the Converter name and method name along with what property it is going to modify?

Edit: I also tried using XamlReader and added a reference to PresentationCore dll but i dont see XamlReader class in System.Windows.Markup namespace.

Upvotes: 0

Views: 360

Answers (1)

Laurence Moroney
Laurence Moroney

Reputation: 1263

Sounds like using XPath would be your friend.

With XPath you can define the path to the nodes you want, with conditionals, and get a set of matching nodes returned to you.

Example of using it in C#: http://support.microsoft.com/kb/308333

XPath Tutorial: http://www.w3schools.com/xpath/

Upvotes: 1

Related Questions