Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8391703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:19:47+00:00 2026-06-09T19:19:47+00:00

Possible Duplicate: I want to load the data from XML file and show in

  • 0

Possible Duplicate:
I want to load the data from XML file and show in the listbox?

I have a three classes:

First is Person:

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

Second is:

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

Third is:

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

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

I have One more class where i just call the data from PeronDB (Class)

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

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

InPersonDB class I am doing if its Student node then read the data if its Teacher nodes then also reads the data and If it Person it also reads the data.

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

And in the Form Load i am doing:

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

It show me “{Unable to cast object of type ‘Collection.Person’ to type ‘Collection.Student’.”} this error 🙁 System.InvalidCastException It show me in PersonDB Class” this error.
Please guys help me How to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:19:48+00:00Added an answer on June 9, 2026 at 7:19 pm

    don’t cast person as student or teacher see below

        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;
        }
       } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: File to byte[] in Java I want to read data from file
Possible Duplicate: Android: ProgressDialog.show() crashes with getApplicationContext I want to have a progress dialog
Possible Duplicate: C++ invoke explicit template constructor First imagine I have a class Data
Possible Duplicate: Load an SWF into a WebView I have a .swf file and
Possible Duplicate: PHP create a file without fopen I want to create a file
Possible Duplicate: Return value from thread I want to get the free memory of
Possible Duplicate: returning multiple values from a function For example if you want a
Possible Duplicate: How to update GUI from another thread in C#? I have form
Possible Duplicate: Open file from complete path under cursor in Vim Let's say I
Possible Duplicate: jquery/ajax load new content when available i have a table named news

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.