在 Windows 窗体中,我在面板中有一些标签,我想显示从listBox1
文件夹加载(.rtdl)文件集合的静态值。
当用户选择每个时,我想labels
在面板中显示相应的属性值。
填充 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);
}
}
从 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
}
}
}
}
当我选择文件时,它会illegal characters in path
在using (XmlReader reader = XmlReader.Create(xmldoc))
.
请告诉我这里有什么问题???