0

可能重复:
我想从 XML 文件加载数据并显示在列表框中?

我有一个三类:

首先是人:

    public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }



    public Person()
    {
    }

    public override string ToString()
    {
        return FirstName + " " +LastName + "\t" +Email;
    }
}

二是:

     public class Student:Person
{

    public Student()
    {
    }
    public double AssessmentGrade { get; set; }
    public double AssignmentGrade { get; set; }

    public override string ToString()
    {
        return base.ToString() + "," +AssessmentGrade + "," + AssignmentGrade;
    }
}

第三是:

   public class Teacher:Person
{
    public int RoomNumber
    {
        get;
        set;
    }

    public override string ToString()
    {
        return base.ToString() + "," + RoomNumber;
    }
}

我还有一门课,我只是从 PeronDB (类)中调用数据

    public class Persons
{
     private List<Person> persons = null;

       public void Fill()
    {
        persons = PersonDB.GetPersons();
    }
 }

InPersonDB 类如果它的 Student 节点然后读取数据,如果它的 Teacher 节点也读取数据并且如果它 Person 它也读取数据,我正在做。

    public class PersonDB
{
    private const string path = @"..\..\Persons.xml";

    public static List<Person> GetPersons()
    {
        List<Person> persons = new List<Person>();

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.IgnoreWhitespace = true;
        settings.IgnoreComments = true;

        XmlReader xmlIn = XmlReader.Create(path, settings);

        if (xmlIn.ReadToDescendant("Student"))
        {
            do
            {
                Person person = new Person();
                Student student = (Student)person;
                xmlIn.ReadStartElement("Student");
                student.FirstName = xmlIn.ReadElementContentAsString();
                student.LastName = xmlIn.ReadElementContentAsString();
                student.Email = xmlIn.ReadElementContentAsString();
                student.AssessmentGrade = xmlIn.ReadElementContentAsInt();
                student.AssignmentGrade = xmlIn.ReadElementContentAsInt();

                persons.Add(student);
            } while (xmlIn.ReadToNextSibling("Student"));

        }

        else if (xmlIn.ReadToDescendant("Teacher"))
        {
            do
            {
                Person person = new Person();
                Teacher teacher = (Teacher)person;
                xmlIn.ReadStartElement("Teacher");
                teacher.FirstName = xmlIn.ReadElementContentAsString();
                teacher.LastName = xmlIn.ReadElementContentAsString();
                teacher.Email = xmlIn.ReadElementContentAsString();
                teacher.RoomNumber = xmlIn.ReadElementContentAsInt();

                persons.Add(teacher);
            } while (xmlIn.ReadToNextSibling("Teacher"));
        }

        if (xmlIn.ReadToDescendant("Person"))
        {
            do
            {
                Person person = new Person();

                xmlIn.ReadStartElement("Person");
                person.FirstName = xmlIn.ReadElementContentAsString();
                person.LastName = xmlIn.ReadElementContentAsString();
                person.Email = xmlIn.ReadElementContentAsString();

                persons.Add(person);
            } while (xmlIn.ReadToNextSibling("Person"));
        }


        xmlIn.Close();

        return persons;
    }
   } 

在表单加载中我正在做:

     Persons personList = null;

        private void FillPersonlstBox()
    {
        Person p;
        listBox1.Items.Clear();
        for (int i = 0; i < personList.Count; i++)
        {
            p = personList[i];
            listBox1.Items.Add(p.ToString());
        }
    }

    private void Load_Click(object sender, EventArgs e)
    {
        personList.Changed += new Persons.ChangeHandler(HandleChange);
        personList.Fill();
        FillPersonlstBox();
    }

它告诉我“{Unable to cast object of type 'Collection.Person' to type 'Collection.Student'。”}这个错误:( System.InvalidCastException 它在 PersonDB 类中显示这个错误。请大家帮我如何解决它?

4

1 回答 1

1

不要将人选为学生或老师,见下文

    public class PersonDB
{
    private const string path = @"..\..\Persons.xml";

    public static List<Person> GetPersons()
    {
        List<Person> persons = new List<Person>();

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.IgnoreWhitespace = true;
        settings.IgnoreComments = true;

        XmlReader xmlIn = XmlReader.Create(path, settings);

        if (xmlIn.ReadToDescendant("Student"))
        {
            do
            {

                Student student = new Student();
                xmlIn.ReadStartElement("Student");
                student.FirstName = xmlIn.ReadElementContentAsString();
                student.LastName = xmlIn.ReadElementContentAsString();
                student.Email = xmlIn.ReadElementContentAsString();
                student.AssessmentGrade = xmlIn.ReadElementContentAsInt();
                student.AssignmentGrade = xmlIn.ReadElementContentAsInt();

                persons.Add(student);
            } while (xmlIn.ReadToNextSibling("Student"));

        }

        else if (xmlIn.ReadToDescendant("Teacher"))
        {
            do
            {

                Teacher teacher = new Teacher();
                xmlIn.ReadStartElement("Teacher");
                teacher.FirstName = xmlIn.ReadElementContentAsString();
                teacher.LastName = xmlIn.ReadElementContentAsString();
                teacher.Email = xmlIn.ReadElementContentAsString();
                teacher.RoomNumber = xmlIn.ReadElementContentAsInt();

                persons.Add(teacher);
            } while (xmlIn.ReadToNextSibling("Teacher"));
        }

        if (xmlIn.ReadToDescendant("Person"))
        {
            do
            {
                Person person = new Person();

                xmlIn.ReadStartElement("Person");
                person.FirstName = xmlIn.ReadElementContentAsString();
                person.LastName = xmlIn.ReadElementContentAsString();
                person.Email = xmlIn.ReadElementContentAsString();

                persons.Add(person);
            } while (xmlIn.ReadToNextSibling("Person"));
        }


        xmlIn.Close();

        return persons;
    }
   } 
于 2012-08-16T04:16:52.243 回答