Serialization Windows Phone 7中的DataContractSerializer问题
这种情况偶尔发生,但会使我的文件无法读取。问题是,在序列化时,DataContractSerializer在XML文件的末尾添加了两个>>>。使它们在尝试反序列化时变得无用。有人有这个问题吗?例如:Serialization Windows Phone 7中的DataContractSerializer问题,serialization,windows-phone-7,Serialization,Windows Phone 7,这种情况偶尔发生,但会使我的文件无法读取。问题是,在序列化时,DataContractSerializer在XML文件的末尾添加了两个>>>。使它们在尝试反序列化时变得无用。有人有这个问题吗?例如: <SomeObject xmlns="someNamespace"> </SomeObject>>> 装载: using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplica
<SomeObject xmlns="someNamespace">
</SomeObject>>>
装载:
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);
}
}
}
问题实际上是这样的:
保存后一个字符串时,它会写在先前保存的字符串的顶部,而旧字符串的结尾仍然保留在那里 解决方案是确保在保存新内容之前删除现有文件或删除其内容 我以前也有过类似的问题,很难发现
顺便说一句,您可能还需要重新考虑有关性能的序列化策略。请查看以了解更多信息。问题实际上在于:
保存后一个字符串时,它会写在先前保存的字符串的顶部,而旧字符串的结尾仍然保留在那里 解决方案是确保在保存新内容之前删除现有文件或删除其内容 我以前也有过类似的问题,很难发现
顺便说一句,您可能还需要重新考虑有关性能的序列化策略。查看了解更多信息。解决了这个问题。几乎是在欺骗。基本上,我使用XDocument读取XML文档,使用XDocument.CreateReader()获取读取器,并将读取器传递给DataContractSerializer。我知道这不是很优雅,但这是一个解决方案。希望将来我能想出一个更好的解决方案。现在,代码如下:
private bool Load()
{
if (!_isLoaded)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
if (file.FileExists(_fileName))
{
string text = string.Empty;
try
{
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(_fileName, FileMode.Open, file))
{
if (stream.Length > 0)
{
XDocument document = GetXDocument(stream);
DataContractSerializer serializer = new DataContractSerializer(typeof(ClubManager));
ClubManager temp = serializer.ReadObject(document.CreateReader()) as ClubManager;
stream.Close();
}
}
_isLoaded = true;
}
catch (Exception ex)
{
MessageBox.Show("There was an error loading your favorite clubs. " + ex.Message);
}
}
}
}
return _isLoaded;
}
private XDocument GetXDocument(IsolatedStorageFileStream stream)
{
// read the file to find errors
string documentText = ReadBytes(stream);
return XDocument.Parse(documentText);
}
private static string ReadBytes(IsolatedStorageFileStream stream)
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
string normal = string.Empty;
string hex = BitConverter.ToString(buffer).Replace("-", "");
while (hex.Length > 0)
{
// Use ToChar() to convert each ASCII value (two hex digits) to the actual character
normal += System.Convert.ToChar(System.Convert.ToUInt32(hex.Substring(0, 2), 16)).ToString();
// Remove from the hex object the converted value
hex = hex.Substring(2, hex.Length - 2);
}
return normal;
}
谢谢 解决了它。几乎是在欺骗。基本上,我使用XDocument读取XML文档,使用XDocument.CreateReader()获取读取器,并将读取器传递给DataContractSerializer。我知道这不是很优雅,但这是一个解决方案。希望将来我能想出一个更好的解决方案。现在,代码如下:
private bool Load()
{
if (!_isLoaded)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
if (file.FileExists(_fileName))
{
string text = string.Empty;
try
{
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(_fileName, FileMode.Open, file))
{
if (stream.Length > 0)
{
XDocument document = GetXDocument(stream);
DataContractSerializer serializer = new DataContractSerializer(typeof(ClubManager));
ClubManager temp = serializer.ReadObject(document.CreateReader()) as ClubManager;
stream.Close();
}
}
_isLoaded = true;
}
catch (Exception ex)
{
MessageBox.Show("There was an error loading your favorite clubs. " + ex.Message);
}
}
}
}
return _isLoaded;
}
private XDocument GetXDocument(IsolatedStorageFileStream stream)
{
// read the file to find errors
string documentText = ReadBytes(stream);
return XDocument.Parse(documentText);
}
private static string ReadBytes(IsolatedStorageFileStream stream)
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
string normal = string.Empty;
string hex = BitConverter.ToString(buffer).Replace("-", "");
while (hex.Length > 0)
{
// Use ToChar() to convert each ASCII value (two hex digits) to the actual character
normal += System.Convert.ToChar(System.Convert.ToUInt32(hex.Substring(0, 2), 16)).ToString();
// Remove from the hex object the converted value
hex = hex.Substring(2, hex.Length - 2);
}
return normal;
}
谢谢 谢谢你的建议。我想到了,所以我决定去做。现在保存之前,每次都会删除该文件,额外的>>字符问题也就消失了。但是我仍然有反序列化问题,即使第一个问题不再是问题。我一定会看看你建议的序列化策略。谢谢你的建议。我想到了,所以我决定去做。现在保存之前,每次都会删除该文件,额外的>>字符问题也就消失了。但是我仍然有反序列化问题,即使第一个问题不再是问题。我一定会看看你建议的序列化策略。
private bool Load()
{
if (!_isLoaded)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
if (file.FileExists(_fileName))
{
string text = string.Empty;
try
{
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(_fileName, FileMode.Open, file))
{
if (stream.Length > 0)
{
XDocument document = GetXDocument(stream);
DataContractSerializer serializer = new DataContractSerializer(typeof(ClubManager));
ClubManager temp = serializer.ReadObject(document.CreateReader()) as ClubManager;
stream.Close();
}
}
_isLoaded = true;
}
catch (Exception ex)
{
MessageBox.Show("There was an error loading your favorite clubs. " + ex.Message);
}
}
}
}
return _isLoaded;
}
private XDocument GetXDocument(IsolatedStorageFileStream stream)
{
// read the file to find errors
string documentText = ReadBytes(stream);
return XDocument.Parse(documentText);
}
private static string ReadBytes(IsolatedStorageFileStream stream)
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
string normal = string.Empty;
string hex = BitConverter.ToString(buffer).Replace("-", "");
while (hex.Length > 0)
{
// Use ToChar() to convert each ASCII value (two hex digits) to the actual character
normal += System.Convert.ToChar(System.Convert.ToUInt32(hex.Substring(0, 2), 16)).ToString();
// Remove from the hex object the converted value
hex = hex.Substring(2, hex.Length - 2);
}
return normal;
}