使用linq to xml过滤作为对象存在的xml数据 [英] Filter xml data present as a object using linq to xml

查看:65
本文介绍了使用linq to xml过滤作为对象存在的xml数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我想过滤xmldata作为动态生成的堆栈面板的项目源,包括textblock,combobox和textbox。我想要的是因为textblock显示所选元素(我用户应该从组合框中选择一个过滤条件并将文本添加到文本框中并基于3个过滤器,它应该过滤并显示过滤后的内容。在搜索互联网时我发现linq到xml是可行的方式。



这里是xaml

codexaml

hello friends,
I want to filter xmldata present as a item source of dynamically generated stack panel consisting of textblock,combobox and textbox.What i want is since textblock displays the selected element(which i have already figured out) user should select a filter criteria from the combobox and add text to the textbox and based on 3 filters,,it should filter and should display the filtered content.On searching the internet i found that linq to xml was a feasilble way.

here is the xaml
codexaml

<ListView Height="222" HorizontalAlignment="Left" Margin="341,24,0,0" Name="listView1" VerticalAlignment="Top" Width="290" removed="Green" 

                  AllowDrop="True" 

                  DragDrop.Drop="listview_drop" 

                  DragDrop.DragEnter="treeview_dragenter" ItemsSource="{Binding XPath=self::*}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" x:Name="stc">
                        <TextBlock Text="{Binding Path=Name}" Margin="0,0,3,0" x:Name="abc"/>
                        <ComboBox Margin="0,0,3,0" x:Name="cbox1">
                            <ComboBoxItem Content="Less Than"/>
                            <ComboBoxItem Content="Greater Than"/>
                            <ComboBoxItem Content="Equals"/>
                        </ComboBox>
                        <TextBox Margin="0,0,3,0" Width="50" x:Name="textbox1" />
                        <Button x:Name="but1" Height="25" Width="35" Click="click" Content="gen" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>







和背后的c#代码






And the c# code behind

private void click(object sender, RoutedEventArgs e)
{
    var button = sender as Button;
    var parent = button.Parent as FrameworkElement;
    var textBox = parent.FindName("textbox1") as TextBox;
    var textbl = parent.FindName("abc") as TextBlock;
    var com=parent.FindName("cbox1")as ComboBox;

    Xelement filter= (Xelement)((XmlDataProvider)Resources["xmlDataProvider"]).Data;

}





我的问题是在使用xelement时,Iam得到一个无效的类型异常,它无法将system.xml.xmldocument转换为system.xml.linq。



My problem is that on working with xelement,Iam getting a invalid type exception that it cannot convert system.xml.xmldocument to system.xml.linq .

推荐答案

Abhinav



这是因为右侧语句返回XDocument,左侧是XElement。



或者,你在左侧采用XDocument,然后将其转换为XElement或使用XDocument。



我希望,这可能解决了这个问题。



谢谢。
Abhinav

This is because the right hand side statement return XDocument and left hand side is XElement.

Either, you take XDocument on left side and then convert it into XElement or use XDocument.

I hope, this might resolved the problem.

Thanks.


这篇关于使用linq to xml过滤作为对象存在的xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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