将复杂对象集合连续序列化到文件

发布于 2025-01-05 11:20:29 字数 532 浏览 2 评论 0原文

我正在开发一个记录复杂系统状态的应用程序。 我必须将这些状态记录到 xml 文件中(使用 xml 序列化)。 该文件用于重放记录的事件并对其进行分析。

上下文:

我的应用程序显示一组复杂的数据(我们称之为视图) 每个视图包含 1 个对象和 2 个对象集合(400 个项目,每个项目大约有 20 个属性)。

应用程序以固定间隔记录数据状态(2 条记录之间的时间可能从 5 秒到 60 分钟不等),所有记录都存储在 VIEWS 集合中,我们将此 VIEW 集合保存到跟踪文件中(使用 xmlserializer 和将我的对象集合序列化为文本编写器)

一切都很好,但是当文件达到大约 20 Mo 时,文件保存可能比 2 个记录事件之间的时间花费更多的时间(文件存储在网络上,有时我们有滞后),我们希望对此进行优化。

所以我的问题是:

是否有一种方法可以将对象附加到文件而无需加载和写入整个文件,或者有一种方法可以优化文件的序列化?

我们必须使用 xml 序列化,因为文件必须可供人类读取通过代码(反序列化)。

I'm working on an application that record the state of a compplex system.
I've to record these states to xml files (using xml serialisation).
This file is used to replay the recorded event and analyse it.

the context:

My application display a set of complex data (we called this a VIEW)
each view contains 1 object and 2 collections of objects (400 items each with about 20 properties).

The application record a state of the data at fixed interval (the time between 2 record may vary from 5 sec to 60 min), all records are stored in a VIEWS collection and we save this collection of VIEW to a trace file (using xmlserializer and serialise my object collection to a text writer)

All works well but when the file reach about 20 Mo, the file save can take more time than the time between 2 record event (file are stored on the network and sometime we have lags) and we wants to optimize this.

So my question is :

Is there a way to append an object to a file without have to load and write the entire file or a way to optimize the serialisation to file?

We have to use xml serialisation because the file must be readable by human & by code (deserialisation).

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

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

发布评论

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

评论(1

英雄似剑 2025-01-12 11:20:29

使用数据库怎么样?这些东西是为了解决这样的问题:)如果你想手动完成,你将必须声明你自己的协议。一个原始的工作实现:

[Serializable]
public class Test
{
    public string Name { get; set; }
}

private static string Serialize(Test instance)
{
    XmlSerializer serializer = new XmlSerializer(typeof(Test));
    using (MemoryStream stream = new MemoryStream())
    {
        serializer.Serialize(stream, instance);
        return Convert.ToBase64String(stream.ToArray());
    }
}
private static Test Deserialize(string instance)
{
    XmlSerializer serializer = new XmlSerializer(typeof(Test));
    byte[] instanceBytes = Convert.FromBase64String(instance);

    using (MemoryStream stream = new MemoryStream(instanceBytes))
    {
        return serializer.Deserialize(stream) as Test;
    }
}



public static void Main(string[] args)
{
    using (MemoryStream stream = new MemoryStream())
    {
        // write some test objects
        StreamWriter writer = new StreamWriter(stream);
        writer.WriteLine(Serialize(new Test() { Name = "test1" }));
        writer.WriteLine(Serialize(new Test() { Name = "test2" }));
        writer.WriteLine(Serialize(new Test() { Name = "test3" }));
        writer.Flush();

        // reset
        stream.Position = 0;

        // read them back again
        StreamReader reader = new StreamReader(stream);
        Test test1 = Deserialize(reader.ReadLine());
        Test test2 = Deserialize(reader.ReadLine());
        Test test3 = Deserialize(reader.ReadLine());
    }
}

How about using a database? Those things are made to solve problems like these :) If you want to do it manually, you will have to declare your own protocol. A primitive working implementation:

[Serializable]
public class Test
{
    public string Name { get; set; }
}

private static string Serialize(Test instance)
{
    XmlSerializer serializer = new XmlSerializer(typeof(Test));
    using (MemoryStream stream = new MemoryStream())
    {
        serializer.Serialize(stream, instance);
        return Convert.ToBase64String(stream.ToArray());
    }
}
private static Test Deserialize(string instance)
{
    XmlSerializer serializer = new XmlSerializer(typeof(Test));
    byte[] instanceBytes = Convert.FromBase64String(instance);

    using (MemoryStream stream = new MemoryStream(instanceBytes))
    {
        return serializer.Deserialize(stream) as Test;
    }
}



public static void Main(string[] args)
{
    using (MemoryStream stream = new MemoryStream())
    {
        // write some test objects
        StreamWriter writer = new StreamWriter(stream);
        writer.WriteLine(Serialize(new Test() { Name = "test1" }));
        writer.WriteLine(Serialize(new Test() { Name = "test2" }));
        writer.WriteLine(Serialize(new Test() { Name = "test3" }));
        writer.Flush();

        // reset
        stream.Position = 0;

        // read them back again
        StreamReader reader = new StreamReader(stream);
        Test test1 = Deserialize(reader.ReadLine());
        Test test2 = Deserialize(reader.ReadLine());
        Test test3 = Deserialize(reader.ReadLine());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文