XmlDataProvider使用默认命名空间读取XML

时间:2014-01-10 14:36:19

标签: c# xml wpf xmldataprovider

我已经意识到我的问题是读取下面的XML文件是因为Namespace,我已经添加了对XmlDataProvider的支持,但我无法让它工作,我还没有找到一种方法来调试它。

任何可以发现我哪里出错的人?

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>

XML文件的开头:

<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 个答案:

答案 0 :(得分:0)

你的问题在这一行:

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

您需要加入.xsd

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

设置XML名称空间前缀:

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

然后你需要在XML元素上使用你的前缀:

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