Windows phone 7 将xml文件保存到独立存储(WP7)时转换缓冲区溢出
我试图将一个xml文件保存到WP7的独立存储中,我使用streamreader下载了该文件,但在save(isostream)语句中出现了转换缓冲区溢出。我怀疑我的xml文件有问题,因为如果我在C#中手动创建一个Xdocument,我的saver方法工作得很好。有人体验过类似的东西吗?谢谢 代码: xml文件Windows phone 7 将xml文件保存到独立存储(WP7)时转换缓冲区溢出,windows-phone-7,isolatedstorage,Windows Phone 7,Isolatedstorage,我试图将一个xml文件保存到WP7的独立存储中,我使用streamreader下载了该文件,但在save(isostream)语句中出现了转换缓冲区溢出。我怀疑我的xml文件有问题,因为如果我在C#中手动创建一个Xdocument,我的saver方法工作得很好。有人体验过类似的东西吗?谢谢 代码: xml文件 - <pactrack version="1.0" date="Sat Jan 21 19:22:48 CET 2012" size="276" lang="SE"> &l
- <pactrack version="1.0" date="Sat Jan 21 19:22:48 CET 2012" size="276" lang="SE">
<header />
- <body>
<programevent level="4">måste anges i sökvärdet kolliid: dfs</programevent>
</body>
<footer />
</pactrack>
我已经能够重现这个问题:
var xmlDoc = XDocument.Load(new MemoryStream(Encoding.Unicode.GetBytes(xml)));
xmlDoc.Declaration = new XDeclaration("1.0", "iso-8859-1", "yes");
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var isoStream = new IsolatedStorageFileStream("test1.xml", FileMode.Create, isoStore))
{
xmlDoc.Save(isoStream);
}
}
我还不明白为什么,但它似乎来自编码器。可能指定的编码器中存在无效字符
无论如何,我认为你可以通过明确地切换到unicode来解决问题:
xmlDoc.Declaration = new XDeclaration("1.0", "UTF-8", "yes");
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("test1.xml", FileMode.Create, isoStore))
{
xmlDoc.Save(isoStream);
}
}
只要确保从隔离存储中读回XML文件时字符仍然正确。我已经能够重现问题:
var xmlDoc = XDocument.Load(new MemoryStream(Encoding.Unicode.GetBytes(xml)));
xmlDoc.Declaration = new XDeclaration("1.0", "iso-8859-1", "yes");
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var isoStream = new IsolatedStorageFileStream("test1.xml", FileMode.Create, isoStore))
{
xmlDoc.Save(isoStream);
}
}
我还不明白为什么,但它似乎来自编码器。可能指定的编码器中存在无效字符
无论如何,我认为你可以通过明确地切换到unicode来解决问题:
xmlDoc.Declaration = new XDeclaration("1.0", "UTF-8", "yes");
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("test1.xml", FileMode.Create, isoStore))
{
xmlDoc.Save(isoStream);
}
}
只需确保从独立存储读回XML文件时字符仍然正确。是否可以用调用堆栈复制完整的异常?嗨,将完整的调用堆栈添加到初始问题。是否可以用调用堆栈复制完整的异常?嗨,将完整的调用堆栈添加到初始问题。嗨,再次。当我发现同样的问题时,我正在写作,IsolatedStorageFileStream不喜欢接受非utf8编码!!我刚刚手动更改了xml头文件的编码XDocument doc=loadedDataISO;doc.Declaration.Encoding=“UTF-8”;XDocument loadedData=doc;但你的解决方案更漂亮!是的,在从记忆中读出来之后,我并没有失去那些特别的角色,只是现在尝试了一下。所以现在一切都可以正常工作了,但也许他们应该将此添加到msdn中,使其不接受utf8以外的任何内容。很高兴在这里没有发现这个问题,因为非英语用户必须经常遇到这个问题?!再次感谢您的帮助!你好。当我发现同样的问题时,我正在写作,IsolatedStorageFileStream不喜欢接受非utf8编码!!我刚刚手动更改了xml头文件的编码XDocument doc=loadedDataISO;doc.Declaration.Encoding=“UTF-8”;XDocument loadedData=doc;但你的解决方案更漂亮!是的,在从记忆中读出来之后,我并没有失去那些特别的角色,只是现在尝试了一下。所以现在一切都可以正常工作了,但也许他们应该将此添加到msdn中,使其不接受utf8以外的任何内容。很高兴在这里没有发现这个问题,因为非英语用户必须经常遇到这个问题?!再次感谢您的帮助!