0

重複の可能性:
XMLファイルからデータをロードしてリストボックスに表示したいですか?

私には3つのクラスがあります:

最初は人です:

    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;
    }
}

2番目は:

     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;
    }
}

3番目は:

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

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

PeronDB(クラス)からデータを呼び出すだけのクラスがもう1つあります

    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();
    }

「{タイプ'Collection.Person'のオブジェクトをタイプ'Collection.Student'にキャストできません。」}このエラー:(System.InvalidCastExceptionPersonDBクラスに表示されます"このエラー。それ?

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 に答える