XmlDataProvider reading XML with default namespace

570 Views Asked by At

I have realized my problem to read the XML file below was because of the Namespace and I have added support for that to the XmlDataProvider but I can't get this to work and I have not found a way to debug this..

Anyone who can spot where I go wrong?

XAML:

    <XmlDataProvider
        x:Key="xmlDataProvider"
        XPath="/r:entityStoreData/r:metaInfo/r:exportRoots" Source="C:\tmp\Data.XML">
        <XmlDataProvider.XmlNamespaceManager>
            <XmlNamespaceMappingCollection>
                <XmlNamespaceMapping 
                   Uri="http://www.vordel.com/2005/06/24/entityStore" 
                   Prefix="r" />
            </XmlNamespaceMappingCollection>
        </XmlDataProvider.XmlNamespaceManager>
    </XmlDataProvider>

    <HierarchicalDataTemplate x:Key="DataTemp" ItemsSource="{Binding XPath=r:key}"  DataType="Key">
        <StackPanel Orientation="Horizontal" Margin="0,2">
            <Image>
                <Image.Style>
                    <Style>
                        <Setter Property="Image.Source" Value="D:\Projects\Icons\PNG\coffee.png" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Source={StaticResource xmlDataProvider}, XPath=@type}" Value="Container">
                                <Setter Property="Image.Source" Value="D:\Projects\Icons\PNG\coffee.png" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Source={StaticResource xmlDataProvider}, XPath=@type}" Value="Configuration">
                                <Setter Property="Image.Source" Value="D:\Projects\Icons\PNG\edit.png" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Source={StaticResource xmlDataProvider}, XPath=@type}" Value="Circuit">
                                <Setter Property="Image.Source" Value="D:\Projects\Icons\PNG\chat.png" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Image.Style>
            </Image>
            <TextBlock Text="{Binding Source={StaticResource xmlDataProvider}, XPath=r:id/@value}" Margin="5,0" />
        </StackPanel>
    </HierarchicalDataTemplate>
</Window.Resources>

<Grid>
    <TreeView Name="TV1" HorizontalAlignment="Left" Height="428" VerticalAlignment="Top" Width="1070" ItemsSource="{Binding}" ItemTemplate="{StaticResource DataTemp}"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="10,445,0,0" VerticalAlignment="Top" Width="116" Height="33" Click="Button_Click_1"/>

</Grid>

Beginning of XML File:

<entityStoreData xmlns="http://www.vordel.com/2005/06/24/entityStore">
<metaInfo flags="138">
    <exportRoots>
        <key type='ESConfiguration'>
            <id field='name' value='Entity Store Configuration'/>
        </key>
        <key type='CircuitContainer'>
            <id field='name' value='Scania'/>
            <key type='CircuitContainer'>
                <id field='name' value='Integrations'/>
                <key type='CircuitContainer'>
                    <id field='name' value='SCIS502_DriverTripService'/>
                    <key type='FilterCircuit'>
                        <id field='name' value='SCPL0035_CheckADGroupMembership_SCIS502'/>
                    </key>
                </key>
            </key>
        </key>
        <key type='XPathGroup'>
            <id field='name' value='XPath Definitions'/>
            <key type='XPathAddNodeLocationGroup'>
                <id field='name' value='Add Node Locations'/>
                <key type='XPath'>
                    <id field='name' value='SOAP 1.2 Header Element'/>
                </key>
            </key>
        </key>
1

There are 1 best solutions below

3
On

Your problem is in this line:

<entityStoreData xmlns="http://www.vordel.com/2005/06/24/entityStore">

You need to include the .xsd:

<entityStoreData xmlns="http://www.vordel.com/2005/06/24/entityStore.xsd">

Set up your XML Namespace Prefix:

<XmlDataProvider Source="/WpfApplication2;component/Xml/TestXMLFile.xml" 
    XPath="es:entityStoreData/es:metaInfo/es:exportRoots/es:key">
    <XmlDataProvider.XmlNamespaceManager>
        <XmlNamespaceMappingCollection>
            <XmlNamespaceMapping Uri="http://www.vordel.com/2005/06/24/entityStore.xsd"
                Prefix="es"/>
        </XmlNamespaceMappingCollection>
    </XmlDataProvider.XmlNamespaceManager>
</XmlDataProvider>

Then you need to use your prefix on the XML elements:

<HierarchicalDataTemplate x:Key="ItemTemplate" ItemsSource="{Binding XPath=es:key}"  DataType="key">
    <StackPanel Orientation="Horizontal" Margin="0,2">
        <Image>
            <Image.Style>
                <Style>
                    <Setter Property="Image.Source" Value="D:\Projects\Icons\PNG\coffee.png" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Source={StaticResource xmlDataProvider}, XPath=@type}" Value="Container">
                            <Setter Property="Image.Source" Value="D:\Projects\Icons\PNG\coffee.png" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Source={StaticResource xmlDataProvider}, XPath=@type}" Value="Configuration">
                            <Setter Property="Image.Source" Value="D:\Projects\Icons\PNG\edit.png" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Source={StaticResource xmlDataProvider}, XPath=@type}" Value="Circuit">
                            <Setter Property="Image.Source" Value="D:\Projects\Icons\PNG\chat.png" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
        <TextBlock Text="{Binding XPath=es:id/@value}" Margin="5,0" />
    </StackPanel>
</HierarchicalDataTemplate>