2013-08-04 23 views
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)。

回答

0

使用XmlSerializer.Serialize和XmlSerializer.Deserialize。這裏不需要XmlReader,可能會導致問題。

例子在這裏: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

編輯:

要打開文件使用FileStream類,並將其作爲參數傳遞給這些方法。從上面的鏈接

報價:

FileStream fs = new FileStream(filename, FileMode.Open); 

EDIT2(關於評論):

對不起,沒注意WP7標籤。我相信這裏的答案對於你現在得到的異常:

Windows Phone 7 : FileStream exception

+0

那我怎麼才能打開一個文件? –

+0

我已經更新了答案。請查看我提供的鏈接中的示例。它在那裏。 – BartoszKP

+0

我改變了代碼: '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) –