1

有一些类:

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) 中存在错误。

4

1 回答 1

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:文件流异常

于 2013-08-04T14:39:12.843 回答