2015-02-09 102 views
0

我試圖讀取一個目錄中存在的多個XML文件(每個XML只有一個記錄)並在DataGridView中顯示記錄。如果任何XML文件使用新數據更新,我希望它的相應條目應該自動更新/刷新DataGridView中的新更新數據。做了一些搜索之後,我發現我可以使用FileSystemWatcher來查找是否有文件被更改,但任何人都可以幫助我如何使用它,我無法找到一個好例子。讀取XML文件以在DataGridView中顯示數據並自動更新UI(如果使用C來更改XML文件)#

我的XML文件看起來像(請注意,它沒有根節點):

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

我的C#代碼讀取XML和填充GridView控件如下:

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. 
      }   
     } 
    } 
} 
+0

試試這個https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx – 2015-02-09 06:02:41

回答

2

你必須在您的課程文件中實施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

+0

雖然這個鏈接可能回答這個問題,但它是更好的在這裏包括答案的基本部分,並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – gknicker 2015-02-09 06:23:40

+0

@gknicker:你真的讓我改變它的代碼 – 2015-02-09 06:26:39