0

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>
StefanE
  • 7,578
  • 10
  • 48
  • 75
  • Can you try to minimize the XML and the XAML so that the error still occurs but there are less possibilities? – Thomas Weller Jan 10 '14 at 14:46
  • You seem to be missing some XML... is that just from this question, or is it missing from your file? *That* would cause a problem. Also, you seem to be missing your XML declaration: `` – Sheridan Jan 10 '14 at 14:55
  • I stated this is the beginning of the XML File. It is properly closed etc but way to big to include the full file.. And also this is an XML from a external product so I'm stuck with how it looks like. – StefanE Jan 10 '14 at 16:20

1 Answers1

0

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>
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • The XML file is being generated from a 3rd party system but I guess its not a big deal to add the .xsd and then remove it later.. But your suggested changes did not help. Is there any way to do debugging when .net is parsing the XML? – StefanE Jan 10 '14 at 16:46
  • Also, I can't compile unless I have x:Key parameter to XmlDataProvider – StefanE Jan 10 '14 at 16:48
  • Of course, you need to ensure that the `XmlDataProvider` is set as the `DataContext` for the `HierarchicalDataTemplate`... I tested this code and it all works as expected. Originally I declared the `XmlDataProvider` in the `UserControl.DataContext` so it didn't need an `x:Key` value. – Sheridan Jan 10 '14 at 17:06