2016-04-05 195 views
-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>

+0

對不起我的錯誤是2和同樣的問題 –

+1

需要查看正在讀入的實際xml,但首先猜測是它缺少xml序言,XML的第一行可能預期爲:'<?xml version =「1.0」encoding =「UTF-8」?>' – DBug

+0

不要把它放在評論中。您需要[編輯]您的問題,並將其添加到可以看到且格式正確的地方。 –

回答

0

試試這個....

首先,你的XML似乎是無效的,我不知道你是否得到';'從但他們不應該在那裏....

下面是一些代碼,將deserialise你的XML到一個列表

Usings

using System; 
using System.Collections.Generic; 
using System.Xml.Serialization; 
using System.IO; 

[XmlRoot(ElementName = "Student")] 
public class Student 
{ 
    [XmlElement(ElementName = "Id")] 
    public string Id { get; set; } 
    [XmlElement(ElementName = "Fname")] 
    public string Fname { get; set; } 
    [XmlElement(ElementName = "Lname")] 
    public string Lname { get; set; } 
} 

代碼

class Program 
{ 
    static void Main(string[] args) 
    { 
     try 
     { 

      List<Student> deserializedList = new List<Student>(); 
      deserializedList = Deserialize<List<Student>>(); 

     }// Put a break-point here, then mouse-over deserializedList 
     catch (Exception) 
     { 

      throw; 
     } 
    } 

    private static T Deserialize<T>() where T : new() 
    { 
     // Create an instance of T 
     T ReturnListOfT = CreateInstance<T>(); 


     // Create a new file stream for reading the XML file 
     using (FileStream ReadFileStream = new FileStream("xml.xml", FileMode.Open, FileAccess.Read, FileShare.Read)) 
     { 
      // Construct a XmlSerializer and use it 
      // to serialize the data from the stream. 
      XmlSerializer SerializerObj = new XmlSerializer(typeof(T)); 
      try 
      { 
       // Deserialize the hashtable from the file 
       ReturnListOfT = (T)SerializerObj.Deserialize(ReadFileStream); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message)); 
      } 

     } 
     // return the Deserialized data. 
     return ReturnListOfT; 
    } 

    // function to create instance of T 
    public static T CreateInstance<T>() where T : new() 
    { 
     return (T)Activator.CreateInstance(typeof(T)); 
    } 
} 

,這是你的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> 

保存XML在同一個文件夾中一個名爲xml.xml爲您的應用程序的* .exe

+0

感謝您的幫助,它的工作 –