當前我正在開發使用隔離存儲處理信息的窗口遊戲。我試圖用XML來實現,但是我在嘗試生成XML文檔並從中讀取它時遇到了這個問題。這是生成的代碼和XML。XML文檔錯誤(4,11)
的代碼部分:
using(IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly())
{
using(IsolatedStorageFileStream stream =
new IsolatedStorageFileStream("class.xml", FileMode.Create, file))
{
XmlWriterSettings setting = new XmlWriterSettings();
setting.Indent = true;
using(XmlWriter writer = XmlWriter.Create(stream, setting))
{
XmlSerializer serializer = new XmlSerializer(typeof (Student));
serializer.Serialize(stream, new Student()
{
Name = "AhLim"
});
}
}
using(IsolatedStorageFileStream stream =
new IsolatedStorageFileStream("class.xml", FileMode.Open, file))
{
XmlSerializer serializer = new XmlSerializer(typeof (Student));
studentA = (Student) serializer.Deserialize(stream);
}
}
Student類:
public class Student
{
public String Name { get; set; }
}
生成的XML文檔:
<?xml version="1.0"?>
<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>AhLim</Name>
</Student>
畢竟,在錯誤的標題,XML文檔錯誤(4,11)發生在反序列化中。我無法找出問題,因爲我google了並知道流問題。感謝您所有的幫助
你的學生對象是否被正確標記爲可序列化? Name屬性是否可寫?你有多個'Student'節點嗎? – slugster
目前我只有一個節點。我可以確保學生對象是可序列化的,並且名稱具有可寫屬性:) –