将xml文件保存到隔离存储时转换缓冲区溢出(WP7)

时间:2012-01-21 18:26:49

标签: windows-phone-7 isolatedstorage

我正在尝试将xml文件保存到我使用streamreader下载的WP7的隔离存储中,但是我在save(isostream)语句中获得了转换缓冲区溢出。我怀疑我的xml文件有问题,因为如果我在C#中手动创建一个Xdocument,我的保护程序工作就完美了。任何经历过类似事情的人?感谢

代码:

 using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("test1.xml", FileMode.Create, isoStore))
                        {
                            xmlDoc.Save(isoStream);
                        }
                    }

xml文件

- <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>

调用堆栈

mscorlib.dll!System.Text.Encoder.Convert(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed) + 0x143 bytes    
    System.Xml.dll!System.Xml.XmlEncodedRawTextWriter.FlushEncoder() + 0x31 bytes   
    System.Xml.dll!System.Xml.XmlEncodedRawTextWriter.Flush() + 0xc bytes   
    System.Xml.dll!System.Xml.XmlWellFormedWriter.Close() + 0x4b bytes  
    System.Xml.dll!System.Xml.XmlWriter.Dispose(bool disposing) + 0x1b bytes    
    System.Xml.dll!System.Xml.XmlWriter.Dispose() + 0x7 bytes   
    System.Xml.Linq.dll!System.Xml.Linq.XDocument.Save(System.IO.Stream stream, System.Xml.Linq.SaveOptions options) + 0x56 bytes   
    System.Xml.Linq.dll!System.Xml.Linq.XDocument.Save(System.IO.Stream stream) + 0xd bytes 
>   panoramaTest.dll!panoramaTest.MainPage.client_DownloadStringCompleted(object sender, System.Net.OpenReadCompletedEventArgs e) Line 49 + 0x8 bytes   C#
    System.Net.dll!System.Net.WebClient.OnOpenReadCompleted(System.Net.OpenReadCompletedEventArgs e) + 0x15 bytes   
    System.Net.dll!System.Net.WebClient.OpenReadOperationCompleted(object arg) + 0xc bytes  
    mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo rtmi, object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object parameters, System.Globalization.CultureInfo culture, bool isBinderDefault, System.Reflection.Assembly caller, bool verifyAccess, ref System.Threading.StackCrawlMark stackMark)   
    mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture, ref System.Threading.StackCrawlMark stackMark) + 0x168 bytes 
    mscorlib.dll!System.Reflection.MethodBase.Invoke(object obj, object[] parameters) + 0xa bytes   
    mscorlib.dll!System.Delegate.DynamicInvokeOne(object[] args) + 0x98 bytes   
    mscorlib.dll!System.MulticastDelegate.DynamicInvokeImpl(object[] args) + 0x8 bytes  
    mscorlib.dll!System.Delegate.DynamicInvoke(object[] args) + 0x2 bytes   
    System.Windows.dll!System.Windows.Threading.DispatcherOperation.Invoke() + 0xc bytes    
    System.Windows.dll!System.Windows.Threading.Dispatcher.Dispatch(System.Windows.Threading.DispatcherPriority priority) + 0x83 bytes  
    System.Windows.dll!System.Windows.Threading.Dispatcher.OnInvoke(object context) + 0x8 bytes 
    System.Windows.dll!System.Windows.Hosting.CallbackCookie.Invoke(object[] args) + 0x19 bytes 
    System.Windows.dll!System.Windows.Hosting.DelegateWrapper.InternalInvoke(object[] args) + 0x2 bytes 
    System.Windows.RuntimeHost.dll!System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(System.IntPtr pHandle, int nParamCount, System.Windows.Hosting.NativeMethods.ScriptParam[] pParams, ref System.Windows.Hosting.NativeMethods.ScriptParam pResult) + 0x5e bytes 
    [External Code] 

1 个答案:

答案 0 :(得分:1)

我已经能够重现这个问题:

        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文件时,请确保字符仍然良好。