Datacontractserializer 不会覆盖所有数据

发布于 2024-10-10 14:49:28 字数 1838 浏览 7 评论 0原文

我注意到,如果我使用 Datacontractserializer 将对象保留回文件中,如果新 xml 的长度比文件中最初存在的 xml 短,则原始 xml 的剩余部分以及新 xml 的长度将保留在文件并会破坏 xml。

有人有一个好的解决方案来解决这个问题吗?

这是我用来持久化对象的代码:

    /// <summary>
    /// Flushes the current instance of the given type to the datastore.
    /// </summary>
    private void Flush()
    {
        try
        {
            string directory = Path.GetDirectoryName(this.fileName);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            FileStream stream = null;
            try
            {
                stream = new FileStream(this.fileName, FileMode.OpenOrCreate);
                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, new System.Text.UTF8Encoding(false)))
                        {
                            stream = null;

                            // The serializer is initialized upstream.
                            this.serializer.WriteObject(writer, this.objectValue);
                        }

                        break;
                    }
                    catch (IOException)
                    {
                        Thread.Sleep(200);
                    }
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
        catch
        {
            // TODO: Localize this
            throw;
            //throw new IOException(String.Format(CultureInfo.CurrentCulture, "Unable to save persistable object to file {0}", this.fileName));
        }
    }

I've noticed that if I persist an object back into file using a Datacontractserializer, if the length of the new xml is shorter than the xml originally present in the file the remnants of the original xml outwith the length of the new xml will remain in the file and will break the xml.

Does anyone have a good solution to fix this?

Here's the code I am using to persist the object:

    /// <summary>
    /// Flushes the current instance of the given type to the datastore.
    /// </summary>
    private void Flush()
    {
        try
        {
            string directory = Path.GetDirectoryName(this.fileName);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            FileStream stream = null;
            try
            {
                stream = new FileStream(this.fileName, FileMode.OpenOrCreate);
                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, new System.Text.UTF8Encoding(false)))
                        {
                            stream = null;

                            // The serializer is initialized upstream.
                            this.serializer.WriteObject(writer, this.objectValue);
                        }

                        break;
                    }
                    catch (IOException)
                    {
                        Thread.Sleep(200);
                    }
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
        catch
        {
            // TODO: Localize this
            throw;
            //throw new IOException(String.Format(CultureInfo.CurrentCulture, "Unable to save persistable object to file {0}", this.fileName));
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一世旳自豪 2024-10-17 14:49:28

这是因为您打开流的方式:

stream = new FileStream(this.fileName, FileMode.OpenOrCreate);

尝试使用:

stream = new FileStream(this.fileName, FileMode.Create);

请参阅 FileMode 文档。

It's because of how you are opening your stream with:

stream = new FileStream(this.fileName, FileMode.OpenOrCreate);

Try using:

stream = new FileStream(this.fileName, FileMode.Create);

See FileMode documentation.

[旋木] 2024-10-17 14:49:28

我相信这是由于使用 FileMode.OpenOrCreate 造成的。如果文件已经存在,我认为该文件正在被打开,并且部分数据正在从起始字节开始被覆盖。如果您更改为使用FileMode.Create,则会强制覆盖所有现有文件。

I believe this is due to using FileMode.OpenOrCreate. If the file already exits, I think the file is being opened and parts of the data are being overwritten from the start byte. If you change to using FileMode.Create it forces any existing files to be overwritten.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文