代码之家  ›  专栏  ›  技术社区  ›  Anuj Sethi

读取XML文件以在DataGridView中显示数据,并在使用C语言更改XML文件时自动更新UI#

  •  0
  • Anuj Sethi  · 技术社区  · 10 年前

    我试图读取一个目录中存在的多个XML文件(每个XML只有一条记录),并在DataGridView中显示这些记录。我希望如果用新数据更新了任何XML文件,则应使用DataGridView中的新更新数据自动更新/刷新相应的条目。在做了一些搜索后,我发现我可以使用FileSystemWatcher来查找是否有任何文件被更改,但请任何人帮助我如何使用它,我找不到一个好的例子。

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

    <?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#代码如下:

    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.
                }           
            }
        }
    }
    
    1 回复  |  直到 10 年前
        1
  •  2
  •   Dhaval Patel    10 年前

    你必须实施 FileSystemWatcher 在你的班级文件中。

    new System.IO.FileSystemWatcher m_Watcher = new System.IO.FileSystemWatcher(); 
    

    然后我们需要为它分配一个路径和一个过滤器,以告诉对象继续查找的位置。

    m_Watcher.Path = "folder path";
    

    接下来,我们需要告诉观察者要看什么。

    m_Watcher.Filter = strFilter;
    

    strFilter应为:

    *.* - Watch all files in the Path *.ext - Watch files with the extension ext name.ext - Watch a particular file name.ext

    接下来,我们需要告诉观察者要寻找什么。

    m_Watcher.NotifyFilter = NotifyFilters.LastAccess | 
                         NotifyFilters.LastWrite | 
                         NotifyFilters.FileName | 
                         NotifyFilters.DirectoryName;
    m_Watcher.IncludeSubdirectories = true; 
    

    接下来,我们需要描述当其中一个属性发生更改时需要做什么。

    m_Watcher.Changed += new FileSystemEventHandler(OnChanged);
    m_Watcher.Created += new FileSystemEventHandler(OnChanged);
    m_Watcher.Deleted += new FileSystemEventHandler(OnChanged);
    m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);
    
    void OnChanged(object sender, FileSystemEventArgs e)
    void OnRenamed(object sender, RenamedEventArgs e)
    

    **最后,我们需要告诉观察者做好自己的工作——观察它**

    m_Watcher.EnableRaisingEvents = true;
    

    您可以使用上述链接。 FileWatcher