-1
我有這樣的代碼,允許用戶閱讀文本文件或XML文件 當他選擇文本文件工作正常,但是當他選擇XML文件 顯示此錯誤: 錯誤在XML文檔(1 ,1)。。當閱讀文本文件
這是我的代碼:
OpenFileDialog op = new OpenFileDialog();
op.Filter = "XML|*.xml|text|*.txt";
if (op.FilterIndex == 1)
{
if (op.ShowDialog() == DialogResult.OK)
{
StreamReader Infile = new StreamReader(op.FileName);
XmlSerializer Des = new XmlSerializer(typeof(List<classname>));
Program.ListStudent = (List<classname>)Des.Deserialize(Infile);
Infile.Close();
}
}
else
{
if (op.FilterIndex == 2)
{
if (op.ShowDialog() == DialogResult.OK)
{
StreamReader Infile = new StreamReader(op.FileName);
string header = Infile.ReadLine();
while (!Infile.EndOfStream)
{
string line = Infile.ReadLine();
string[] parts = line.Split(new char[] { '\t' }, System.StringSplitOptions.RemoveEmptyEntries);
s.Id = Convert.ToInt64(parts[0]);
s.Fname = parts[1];
s.Lname = parts[2];
Program.ListStudent.Add(s);
}
Infile.Close();
}
}
}
這是XML文件
<ArrayOfStudent xmlns:xsi="w3.org/2001/XMLSchema-instance"; xmlns:xsd="w3.org/2001/XMLSchema">; <Student> <Id>12345</Id> <Fname>Mohammad</Fname> <Lname>Ali</Lname> </Student> </ArrayOfStudent>
對不起我的錯誤是2和同樣的問題 –
需要查看正在讀入的實際xml,但首先猜測是它缺少xml序言,XML的第一行可能預期爲:'<?xml version =「1.0」encoding =「UTF-8」?>' – DBug
不要把它放在評論中。您需要[編輯]您的問題,並將其添加到可以看到且格式正確的地方。 –