使用C#读取XML文件以在DataGridView中显示数据,并在XML文件更改时自动更新UI [英] Using C# Read XML files to display data in DataGridView and update UI automatically if XML file is changed

查看:398
本文介绍了使用C#读取XML文件以在DataGridView中显示数据,并在XML文件更改时自动更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在开发一个C#应用程序并尝试读取一个目录中存在的多个XML文件(每个XML只有一个记录)并在DataGridView中显示记录。我想如果任何XML文件使用新数据更新,其相应的条目应该使用DataGridView中的新更新数据自动更新/刷新。在做了一些搜索之后,我发现我可以使用FileSystemWatcher来查找是否有任何文件被更改,但任何人都可以帮助我如何使用它,我无法找到一个好的例子。



我的XML文件看起来像(请注意,其中没有根节点):



Hi All,
I am developing a C# application and trying to read multiple XML files present in one directory (each XML have only one record) and display the records in DataGridView. I want if any of the XML file is updated with new data, its corresponding entry should be automatically updated/refreshed with new updated data in the DataGridView. After doing some search I found that I can use FileSystemWatcher to find if any file is changed, but can anyone please help how can I use it, I am unable to find a good example.

My XML files looks like (please note that there is no root node in it):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<info>
	<id>001</id>
	<name>xyz</name>
	<salary>1000</salary>
	<phone>1234567890</phone>
</info>





用于读取XML和填充gridview的C#代码如下:





My C# code to read XML and populate gridview is as follows:

using System.Xml;
using System.IO;

namespace XML_Reader
{
	public partial class Form1 : Form
	{        
		string[] fileArray;
		string directory_path = "C:\\Users\\XYZ\\Desktop\\test\\";        

		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			//Add columns to empty datagridview
			dataGridView1.Columns.Add("id", "id");
			dataGridView1.Columns.Add("name", "name");
			dataGridView1.Columns.Add("salary", "salary");
			dataGridView1.Columns.Add("phone", "phone");
			
			populateRecords();

		}

		private void populateRecords()
		{
			DataSet ds;
			DataGridViewRow dg_row;

			//Read all XML files and records to datagrid
			fileArray = Directory.GetFiles(directory_path, "MyFiles*.xml");
			foreach (string xmlFile in fileArray_collection)
			{
				//Read the XML from the file                
				ds = new DataSet();
				ds.ReadXml(xmlFile);
				
				//create new row for datagrid
				dg_row = (DataGridViewRow)dataGridView1.Rows[0].Clone();

				//assign values to cells in the new row
				dg_row.Cells[0].Value = ds.Tables["info"].Rows[0]["id"];
				dg_row.Cells[1].Value = ds.Tables["info"].Rows[0]["name"];
				dg_row.Cells[2].Value = ds.Tables["info"].Rows[0]["salary"];
				dg_row.Cells[3].Value = ds.Tables["info"].Rows[0]["phone"];                

				//Add the new row to datagrid -- THE MOMENT RECORD IS DETECTED
				dataGridView1.Rows.Add(dg_row);

				dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count - 1;//This will keep the last added row visible with vertical scrollbar being at bottom.
			}			
		}
	}
}

推荐答案

WEB :
http://www.aspsnippets.com/Articles/Read-XML-File-and-bind-it-to-GridView-in-ASPNet.aspx

Windows :
http://www.codeproject.com/Tips/426762/XML-to-DataSet-or-GridView-and-back


MSDN FileSystemWatcher类



MSDN上大多数类的规范都有示例页面底部。这很简单,只需要调用FileSystemWatcher的实例,设置路径并在应该调用它时进行过滤,然后绑定要在哪个事件上执行的方法。
MSDN FileSystemWatcher Class

Most specifications of classes at MSDN have example at the bottom of the page. It's simple, just make instance of FileSystemWatcher, setup path and filter when it should be called and just bind methods you want to execute upon which event.


这篇关于使用C#读取XML文件以在DataGridView中显示数据,并在XML文件更改时自动更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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