0

我已经意识到读取下面的 XML 文件的问题是因为命名空间,我已经向 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>
4

1 回答 1

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>
于 2014-01-10T15:22:14.023 回答