使用 XPath 过滤来自 XmlDataProvider 的 XML 数据 [英] Filter XML data from XmlDataProvider using XPath

查看:34
本文介绍了使用 XPath 过滤来自 XmlDataProvider 的 XML 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的 xml 文件:

I have a xml file like this:

<Organizations xmlns="">
    <Organization  Title ="dfs" Score="70">
        <Layer  Title ="lar"></Layer>
    </Organization>
    <Organization  Title ="srwrg" Score="70">
        <Layer  Title ="asdf"></Layer>
        <Layer  Title ="lfdkj"></Layer>
    </Organization>
</Organizations>

我在我的 WPF 项目中使用这个文件:(这里是一个资源)

I use this file in my WPF project: (here is a Resource)

<XmlDataProvider x:Key="myData" Source="Data\farsDataOrganization.xml" XPath="//Organizations/Organization"/>

<CollectionViewSource x:Key="MyCVS" >
        <CollectionViewSource.Source>
            <Binding Source="{StaticResource myData}"/>
        </CollectionViewSource.Source>
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="@Title" Direction="Ascending"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

我知道如何根据组织的Title或图层的Title制作过滤器:

I know how to make filter based on organization's Title or Layer's Title:

 XmlDataProvider p = (XmlDataProvider)this.FindResource("myData");
 p.XPath = string.Format("//Organizations/Organization[Layer[contains(@Title,\"{0}\")]]", this.layerNameFilter.Text);

但是我想根据组织的Title 和Layer 的Title 过滤这些数据.有什么建议吗?如何更改我的 XPath 字符串以便我获得基于它们的过滤器?

But I want to filter these data based on both the organization's Title and Layer's Title. Is there any suggestion? How to change my XPath string so that I get the filter based on both of them?

编辑

我在 TreeView 上显示这些数据:

I show these data on a TreeView:

 <TreeView Grid.Row="1" ItemsSource="{Binding Source={StaticResource MyCVS}}">
     <TreeView.Resources>
         <HierarchicalDataTemplate DataType="Organization" ItemsSource="{Binding XPath=Layer}">
             <StackPanel Orientation="Vertical" Width="340" Margin="0,0,0,5">
                 <Label Content="{Binding XPath=@Title}" FontSize="13" Margin="0,0,0,-5"></Label>
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

推荐答案

XmlDataProvider p = (XmlDataProvider)this.FindResource("myData");
p.XPath = string.Format("//Organizations/Organization[contains(@Title,\"{0}\")]/Layer[contains(@Title,\"{1}\")]", 
                        this.orgTitleFilter.Text, 
                        this.layerNameFilter.Text);

这篇关于使用 XPath 过滤来自 XmlDataProvider 的 XML 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆