Reputation: 1939
I have a ListBox
& it has some files. I have 2 Panels
in the same form & each Panel
has many Labels
which are corresponding tags of the loaded file in the ListBox
.
Whenever the user selects each file then display the corresponding data of the selected file in the panel.
For an example this is one of the file content:
<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>
The listBox 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);
}
}
How can I read and display data? Some one please explain to me how can I read/parse xml files in a directory and display data????
Upvotes: 0
Views: 1844
Reputation: 2634
This should get you started if I understand right.
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
}
EDIT:
Actually a switch might be better:
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:
So the listbox contains the file name.
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
}
}
}
}
Upvotes: 1