使用 XPath 从 XmlDataProvider 筛选 XML 数据

本文关键字:XML 数据 筛选 XmlDataProvider XPath 使用 | 更新日期: 2023-09-27 17:56:07

我有一个这样的xml文件:

<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 项目中使用此文件:(这里是资源)

<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制作过滤器:

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

我想根据组织的Title和图层的Title来过滤这些数据。有什么建议吗?如何更改我的XPath字符串,以便我获得基于它们两者的过滤器?

编辑

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

使用 XPath 从 XmlDataProvider 筛选 XML 数据

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