0

我有一個ListBox &它有一些文件。我有2​​在相同的形式&每個Panel有很多Labels這是ListBox加載文件的相應標記。從ListBox中讀取XML文件內容C#win表格

每當用戶選擇每個文件,然後在面板中顯示所選文件的相應數據。

有關示例是這樣的文件內容的一個:

<connection> 
    <sourceId>sdfsdf</sourceId> 
    <description>test.sdfds.interact.loop.com</description> 
    <uri>https://test.sdf.interact.loop.com/WITSML/Store/Store.asmx</uri> 
    <username>sdfdsf</username> 
    <organizationFilter>*</organizationFilter> 
    <fieldFilter>*</fieldFilter> 
    </connection> 

列表框1:

private void Form1_Load(object sender, EventArgs e) 
     { 
      PopulateListBox(listbox1, @"C:\TestLoadFiles", "*.rtld"); 

     } 

private void PopulateListBox(ListBox lsb, string Folder, string FileType) 
     { 
      DirectoryInfo dinfo = new DirectoryInfo(Folder); 
      FileInfo[] Files = dinfo.GetFiles(FileType); 
      foreach (FileInfo file in Files) 
      { 
       lsb.Items.Add(file.Name); 
      } 
     } 

我怎樣才能讀取和顯示數據?有人請向我解釋如何讀取/解析目錄中的xml文件並顯示數據?

+0

是文件xml文件嗎?你想如何顯示文件?您可以將數據放入RichTextBox中。 –

+0

@Nick S .:我編輯了這個問題,並添加了完整的XML文件。我想閱讀某些內容,然後將其顯示在相應的標籤中。你可以幫我嗎??? – linguini

+0

那麼你需要該文件的路徑,然後使用xmlReader打開文件:http://msdn.microsoft.com/en-us/library/cc189056(v=vs.95).aspx然後,你可以通過該文件,然後對於每個屬性/元素,檢查是否要爲該標籤顯示什麼,然後設置標籤文本。如果你有多少個標籤,就會有點長xml attributes/elements –

回答

1

這應該讓你開始,如果我理解的權利。

string path = "C:\\TestLoadFiles.xml"; 
string xmldoc = File.ReadAllText(path); 

using (XmlReader reader = XmlRead.Create(xmldoc)) 
{ 
    reader.MoveToContent(); 
    label_sourceId.Text = reader.GetAttribute("sourceId"); 
    label_description.Text = reader.GetAttribute("description"); 
    // ... for each label if everything will always be the same 
    // might be better to read in the file, verify it, then set your labels 
} 

編輯:
其實一個開關可能會更好:

while (reader.MoveToNextAttribute()) 
{ 
    switch (reader.Name) 
    { 
    case "description": 
     if (!string.IsNullOrEmpty(reader.Value)) 
     label_description.Text = reader.Value; 
     break; 
    case "sourceId": 
     if (!string.IsNullOrEmpty(reader.Value)) 
     label_sourceId.Text = reader.Value; 
     break; 
    // ... 
    } 
} 

EDIT2:

因此列表框包含文件名。

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
    string path = (string)listBox1.SelectedItem; 
    DisplayFile(path); 
} 
private void DisplayFile(string path) 
{ 
    string xmldoc = File.ReadAllText(path); 

    using (XmlReader reader = XmlRead.Create(xmldoc)) 
    { 

     while (reader.MoveToNextAttribute()) 
     { 
      switch (reader.Name) 
      { 
      case "description": 
       if (!string.IsNullOrEmpty(reader.Value)) 
       label_description.Text = reader.Value; // your label name 
       break; 
      case "sourceId": 
       if (!string.IsNullOrEmpty(reader.Value)) 
       label_sourceId.Text = reader.Value; // your label name 
       break; 
      // ... continue for each label 
      } 
     } 
    } 
} 
+0

循環瀏覽文件夾中的文件: 'string [] fileEntries = Directory.GetFiles(folder_path);' –

+0

'private void listBox1_SelectedIndexChanged(object sender,EventArgs e){}'所以,我會將您的代碼添加到listBox1 ???你能否請編輯你的代碼...因爲當我加載你的代碼時,我在xmlreader中出錯。 – linguini

+0

列表框中是否包含文件路徑? –