1
有一些clases:本地XML到列表框在C#
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;
}
我試圖把學生的名字listboks:
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>
但最終我收到一個錯誤
Could not find schema information for the element 'StudentBody'.
什麼我做錯了,如何把數據listboks?
編輯:
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)。
那我怎麼才能打開一個文件? –
我已經更新了答案。請查看我提供的鏈接中的示例。它在那裏。 – BartoszKP
我改變了代碼: 'FileStream fs = new FileStream(@「XMLFile1.xml」,FileMode.Open); XmlSerializer xml = new XmlSerializer(typeof(StudentBody)); // here error StudentBody StudentBody = xml.Deserialize(fs)as StudentBody; StudentsList.ItemsSource = StudentBody.Students;' 但現在有另一個錯誤: 嘗試訪問失敗的方法:System.IO.FileStream..ctor(System.String,System.IO.FileMode) –