【发布时间】:2018-07-26 02:43:47
【问题描述】:
我在我的 Windows 窗体应用程序中单击一个按钮,它会打开一个文件打开框。我单击要打开的 xml 文件,并且希望数据填充 Windows 窗体中的文本字段,但我得到 System.ArgumentException: 'Illegal characters in path。 FileStream 代码行出错。
private void button2_Click(object sender, EventArgs e)
{
// On click Open the file
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog1.FileName);
XmlSerializer serializer = new XmlSerializer(typeof(ContactLead));
// Save file contents to variable
var fileResult = sr.ReadToEnd();
FileStream myFileStream = new FileStream(fileResult, FileMode.Open, FileAccess.Read, FileShare.Read);
ContactLead contactLead = (ContactLead)serializer.Deserialize(myFileStream);
this.textboxFirstName.Text = contactLead.firstname;
this.textboxLastName.Text = contactLead.lastname;
this.textboxEmail.Text = contactLead.email;
}
}
【问题讨论】: