0

ListBoxはいくつかのファイルを持っています。私はPanels同じフォームに2つあり、それぞれにロードされたファイルの対応するタグPanelがたくさんあります.LabelsListBox

ユーザーが各ファイルを選択するたびに、選択したファイルの対応するデータがパネルに表示されます。

たとえば、これはファイル コンテンツの 1 つです。

  <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ファイルを読み取り/解析し、データを表示する方法を教えてください????

4

1 に答える 1

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
           }
        }
    }
} 
于 2012-05-09T15:08:48.427 に答える