XML文档中的错误(1,1)。当读取文本文件时

时间:2016-04-05 23:28:48

标签: xml

我有这段代码,允许用户读取文本文件或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文件

&#13;
&#13;
   
  <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> 
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

试试这个......

首先,您的XML似乎无效,我不知道您是否正在使用&#39 ;;&#39;但他们不应该在那里......

以下是一些将您的XML反序列化为List

的代码

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保存到与应用程序* .exe

相同的文件夹中名为xml.xml的文件中
相关问题