In windows forms, I have some labels in the panel and I would like to display the static values from the listBox1
where it loads collection of (.rtdl) files from a folder.
When user selects each then I want to display the corresponding attribute values to the labels
in the panel.
Code to populate listBox1:
private void Form1_Load(object sender, EventArgs e)
{
PopulateListBox(listBox1, @"C:\TestLoadFiles\", "*.rtdl");
}
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);
}
}
Code to read files from the listBox1:
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
FileInfo file = (FileInfo)listBox1.SelectedItem;
DisplayFile(file.FullName);
string path = (string)listBox1.SelectedItem;
DisplayFile(path);
}
private void DisplayFile(string path)
{
string xmldoc = File.ReadAllText(path);
using (XmlReader reader = XmlReader.Create(xmldoc))
{
while (reader.MoveToNextAttribute())
{
switch (reader.Name)
{
case "description":
if (!string.IsNullOrEmpty(reader.Value))
label5.Text = reader.Value; // your label name
break;
case "sourceId":
if (!string.IsNullOrEmpty(reader.Value))
label6.Text = reader.Value; // your label name
break;
// ... continue for each label
}
}
}
}
When I select the file, it's throwing this error illegal characters in path
at using (XmlReader reader = XmlReader.Create(xmldoc))
.
Please tell me what is wrong here???