使用AsStreamForWrite,间歇性额外字符保留在文件XML文件的末尾 [英] Intermittent extra characters remains at end of file XML file using AsStreamForWrite

查看:113
本文介绍了使用AsStreamForWrite,间歇性额外字符保留在文件XML文件的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用下面的代码片段将对象写入XML文件。它大部分时间都很好用。但有时在XML文件的末尾会有额外的章程。 通常,应用程序正在更新,即使用更新的内容在XML文件
的顶部进行写入。并且看起来"额外字符"是指"额外字符"。从被覆盖的内容中遗留下来。 


此代码还有哪些内容可用于编写新的文件结束标记?

 IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite); 
using(Stream outStream = Task.Run(()=> writeStream.AsStreamForWrite())。Result)
{
serializer.Serialize(outStream,Obj);
outStream.Flush();
outStream.Dispose();
}

谢谢,


Rick


 app.local .folder.createFileAsync(" thefile.xml",Windows.Storage.CreationCollisionOption.replaceExisting).done(function(file){




My app uses the code fragment below to write an object to an XML file. It works most of the time just fine. But occasionally there are extra charters at the end of the XML file.  Usually, the app is updating, i.e. writing over the top of the XML file with updated content. And it appears that the "extra characters" are left over from the contents being overwritten. 

Is there something in addition to this code that should be used to write a new end-of-file marker?

                    IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite);
                    using (Stream outStream = Task.Run(() => writeStream.AsStreamForWrite()).Result)
                    {
                        serializer.Serialize(outStream, Obj);
                        outStream.Flush();
                        outStream.Dispose();
                    }

Thanks,

Rick

解决方案

Use the create flag to overwrite the file:

app.local.folder.createFileAsync("thefile.xml",Windows.Storage.CreationCollisionOption.replaceExisting).done(function (file) {



这篇关于使用AsStreamForWrite,间歇性额外字符保留在文件XML文件的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆