首先是人:
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;
}
}
我還有一個類,其中i只需調用來自PeronDB(Class)的數據
public class Persons
{
private List<Person> persons = null;
public void Fill()
{
persons = PersonDB.GetPersons();
}
}
InPersonDB class如果其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;
}
}
並在窗體的Load我在做什麼:
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();
}
它告訴我在** personList.Changed + =新Persons.ChangeHandler(HandleChange); **這行錯誤。它顯示我「對象引用未設置爲對象的實例」。這個錯誤。 請大家幫幫我如何解決它?
personList =新的人員(); – 2012-08-16 02:41:09
變量personList爲null。你應該實例化,但你使用它。 – 2012-08-16 02:46:28
現在它給我**「{無法投型‘Collection.Person’的對象鍵入‘Collection.Student’。」} **此錯誤:(System.InvalidCastException它告訴我在PersonDB – user1050667 2012-08-16 03:11:07