c#按属性过滤xml文件并在datagridview中显示 [英] c# filter xml file by attribute and show in datagridview

查看:84
本文介绍了c#按属性过滤xml文件并在datagridview中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了并且找不到答案,我确定我只是在寻找正确的关键字,哈哈。



我有一个XML我在datagridview中显示的文件,一切正常。我试图植入可以单击按钮的位置,然后仅使用xml文件的某些部分重新加载 datagridview。



我会更好地描述。只是忍受我,我不是最擅长解释。



这是我的xml文件的布局。

 <?xml version = 1.0?> 
< Movies>
< Name Name = Saw Type = Horror Year = 2004-10-29 Overview = Overview> Saw< / Name>
< Name Name = Saw II Type = Horror Year = 2005-10-28 Overview = Overview> Saw II< / Name>
< Name Name = Speed Type = Action Year = 1994-06-10 Overview = Overview> Speed< / Name>
< Name Name = Batman Begins Type =动作 Year = 2005-06-17 Overview = Overview> Batman Begins< / Name>
< / Movies>

正常显示时,我将其显示在网格视图中。
我不得不将图片上传到该网站的其他位置,因此我无法将其放在此处。
在此处输入链接描述



<现在我想做的是单击按钮时我只能显示某些电影类型的地方。例如,单击一个按钮,仅显示动作的电影。



我已找到有关此信息,并在此处只能选择动作。我让它们弹出在消息框中。

  XmlDocument xml = new XmlDocument(); 
xml.Load( movie.xml);

XmlNodeList xnList = xml.SelectNodes( / Movies / Name [@ Type =’Action’]);
foreach(xnList中的XmlNode xn)
{
MessageBox.Show(xn.InnerText);
}

实际上是从图像中说出了2部动作电影。但是我无法弄清楚如何仅使用这些条目来重新加载datagridview,并且更喜欢像以前一样使用所有信息。



如果我没有清楚解释,请这么说,就像我之前说的那样,我不是最擅长于解释事情。

解决方案

一种方法是将xml加载到

  DataSet ds = new DataSet();可以使用它的内存过滤功能来过滤网格。 
ds.ReadXml( Movies.xml);
this.dataGridView.DataSource = ds.Tables [0];

然后可以像这样过滤

  private void FilterByType(string type)
{
var dataView =((DataTable)this.dataGridView.DataSource).DefaultView;
dataView.RowFilter =(Type =' + type +');
}

要使用子句过滤,请使用以下语法

  dataView.RowFilter =(Type LIKE *' +子字符串+ *') ; 


I have looked and can't find the answer, I'm sure I'm just not looking for the right keywords, lol.

I have an XML file that I display in a datagridview, this all works fine. I'm trying to implant where I can click a button and 'reload' the datagridview with only certain parts of the xml file.

I will describe better. Just bear with me, I'm not the best at explaining.

This is the layout of my xml file.

<?xml version="1.0"?>
<Movies>
  <Name Name="Saw" Type="Horror" Year="2004-10-29" Overview="Overview">Saw</Name>
  <Name Name="Saw II" Type="Horror" Year="2005-10-28" Overview="Overview">Saw II</Name>
  <Name Name="Speed" Type="Action" Year="1994-06-10" Overview="Overview">Speed</Name>
  <Name Name="Batman Begins" Type="Action" Year="2005-06-17" Overview="Overview">Batman Begins</Name>
</Movies>

When I display normally I have it in a grid view like so. I had to upload image elsewhere the site wouldn't let me put it here. enter link description here

Now what I'm trying to do is make is where when I click a button I can display only certain Movie Types. Say for example, click a button and only display movies that are "Action".

I have found information on this and got where I can select only the actions. I got them to pop up in messageboxs'

                XmlDocument xml = new XmlDocument();
            xml.Load("movie.xml");

            XmlNodeList xnList = xml.SelectNodes("/Movies/Name[@Type='Action']");
            foreach (XmlNode xn in xnList)
            {
                MessageBox.Show(xn.InnerText);
            }

This actually shows say from the image the 2 movies that are action. But I can't figure out how to reload the datagridview with only these entries, and prefer with all information as before.

If I didn't explain clearly please say so, as I said before I know I"m not the best at explaining things.

解决方案

One way is to load the xml into a DataTable. Then you can use its in-memory filtering capability to filter the grid.

DataSet ds = new DataSet();
ds.ReadXml("Movies.xml");
this.dataGridView.DataSource = ds.Tables[0];

Then you can filter it like this

private void FilterByType(string type)
{
            var dataView = ((DataTable) this.dataGridView.DataSource).DefaultView;
            dataView.RowFilter = "(Type = '" + type + "')";
}

To filter by substring use this syntax

dataView.RowFilter = "(Type LIKE *'" + substring + "*')";

这篇关于c#按属性过滤xml文件并在datagridview中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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