有一些类:
public class Student : INotifyPropertyChanged
{
string fullName;
string firstName;
string middleName;
string lastName;
string sex;
string photoFilename;
decimal gradePointAverage;
public string FullName
{
set
{
if (fullName != value)
{
fullName = value;
OnPropertyChanged("FullName");
}
}
get
{
return fullName;
}
}
...
protected virtual void OnPropertyChanged(string propChanged)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propChanged));
}
public event PropertyChangedEventHandler PropertyChanged;
}
和:
public class StudentBody : INotifyPropertyChanged
{
private string school;
ObservableCollection<Student> students = new ObservableCollection<Student>();
public string School
{
...
}
public ObservableCollection<Student> Students
{
set
{
if (students != value)
{
students = value;
}
}
get
{
return students;
}
}
private void NotifyPropertyChanged(string propertyName)
{
if (null != PropertyChanged)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
我正在尝试将学生的姓名放入列表中:
ObservableCollection<StudentBody> StudentBody = new ObservableCollection<StudentBody>();
XmlSerializer serializer = new XmlSerializer(typeof(StudentBody));
XmlReader reader = XmlReader.Create(@"XMLFile1.xml");
StudentsList.ItemsSource = StudentBody;
来自 tnis XML:
<?xml version="1.0" encoding="utf-8"?>
<StudentBody xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<School>El Paso High School</School>
<Students>
<Student>
<FullName>Adkins Bowden</FullName>
...
</Student>
<Student>
...
</Student>
<Student>
...
</Student>
</Students>
</StudentBody>
但最后我收到一个错误
找不到元素“StudentBody”的架构信息。
我做错了什么以及如何将数据放入列表中?
编辑:
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var istream = new IsolatedStorageFileStream("XMLFile1.xml", FileMode.OpenOrCreate, store))
{
XmlSerializer xml = new XmlSerializer(typeof(StudentBody));
StudentBody StudentBody = xml.Deserialize(istream) as StudentBody; // here error
StudentsList.ItemsSource = StudentBody.Students;
}
}
错误:XML 文档 (0, 0) 中存在错误。