解决了。在这件事上作弊了。我基本上是用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;
}
谢谢!