这种情况有时只会发生,但会使我的文件无法读取。问题是DataContractSerializer在序列化时在XML文件的末尾添加了几个>>>。在尝试反序列化时使它们无用。有没有人有这个问题?例如:Windows Phone 7中的DataContractSerializer问题
<SomeObject xmlns="someNamespace">
</SomeObject>>>
谢谢。
编辑:
其实也没什么,这不是问题。该文件是罚款这个时候,这是什么样子:
<FavoriteClubManager xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/TicketingWP7.Preferences">
<FavoriteClubs xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:KeyValueOfstringboolean>
<d2p1:Key>XXX</d2p1:Key>
<d2p1:Value>true</d2p1:Value>
</d2p1:KeyValueOfstringboolean>
</FavoriteClubs>
</FavoriteClubManager>
这是尝试反序列化它,当我得到的错误:
“有反序列化类型TicketingWP7的对象错误。偏好.FavoriteClubManager。根元素缺失。“
但我没有看到任何文件错误。
代码
节能:
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(_fileName, FileMode.Create, file))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(FavoriteClubManager));
serializer.WriteObject(stream, this);
}
}
catch (Exception ex)
{
MessageBox.Show("There was an error saving your favorite clubs. " + ex.Message);
}
}
加载:
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
if (file.FileExists(_fileName))
{
try
{
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(_fileName, FileMode.Open, file))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(FavoriteClubManager));
FavoriteClubManager temp = serializer.ReadObject(stream) as FavoriteClubManager;
stream.Close();
}
_isLoaded = true;
}
catch (Exception ex)
{
MessageBox.Show("There was an error loading your favorite clubs. " + ex.Message);
}
}
}
ehm和代码? – 2010-11-04 00:44:37