You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中将实例列表数据保存到文件?

有限元工程程序的数据持久化方案咨询

我开发了一款基于**有限元法(Finite-Element Method)**的工程程序,通过AutoCAD和Abaqus的API获取数据,完成复杂计算后希望将结果保存到文件,方便后续调用。当前简化的数据结构如下:

class TitaniumBar
{
    // 以下数据通过API从其他程序获取
    public Dictionary<int,int> Coordinate { get; set; }
    public Dictionary<string, Dictionary<double, double>> ExternalStress { get; set; }

    // 以下数据为本软件复杂计算后得到的结果
    public double StressRatio { get; set; }
    public Dictionary<long, long[]> ConnectivityData;

    public TitaniumBar()
    {
        Coordinate = new Dictionary<int, int>();
        ExternalStress = new();
        ConnectivityData = new();
    }
}

internal class Program
{
    public static List<TitaniumBar> TitaniumBars { get; set; }
    static void Main(string[] args)
    {
        TitaniumBars = new List<TitaniumBar>();
        for (int i = 0; i<50; i++)
        {
            // 实际数据从其他程序获取,此处为示例
            TitaniumBar bar = new TitaniumBar();
            bar.Coordinate.Add(1, 2);
            Dictionary<double, double> StressData1 = new Dictionary<double, double>() { { 9.5685, 123.123 } };
            Dictionary<double, double> StressData2 = new Dictionary<double, double>() { { 7.366915, 4.59623 } };
            bar.ExternalStress.Add("S1", StressData1);
            bar.ExternalStress.Add("S2", StressData2);

            // 实际数据为复杂计算结果,此处为示例
            bar.StressRatio = 0.9599;
            bar.ConnectivityData.Add(14, new long[] {1,3,4,5});

            TitaniumBars.Add(bar);
        }
        
    }
}

我定义了TitaniumBar类,其中包含通过API获取的Coordinate、ExternalStress数据,以及计算得到的StressRatio、ConnectivityData结果。程序会处理大量这类数据,示例中仅创建了50个实例并存储到TitaniumBars列表中。

我需要将TitaniumBars列表的数据保存到文件以便后续打开使用,请问有哪些可行方案?

注意:我不想使用需要SQL Server的数据库,因为分发程序给同事时过于复杂。我了解JSON或XML序列化可以实现,但还有其他替代方案吗?听说可以使用实体数据库,但不确定是否无需运行SQL Server即可保存文件?


可行方案

1. 二进制序列化(.NET原生/第三方库)

  • 可以使用.NET原生的二进制序列化工具(注意BinaryFormatter已标记过时,推荐使用System.Text.Json的二进制模式或第三方库如MessagePack),将对象序列化为二进制文件。优点是体积小、读写速度快,适合大量数据存储;缺点是类结构变更后可能出现反序列化兼容问题。
  • 示例(使用MessagePack):
using MessagePack;
using System.IO;

// 保存数据
var bytes = MessagePackSerializer.Serialize(Program.TitaniumBars);
File.WriteAllBytes("bars.bin", bytes);

// 读取数据
var bytes = File.ReadAllBytes("bars.bin");
Program.TitaniumBars = MessagePackSerializer.Deserialize<List<TitaniumBar>>(bytes);

2. 文件型嵌入式数据库(无需独立服务)

这类数据库以单个文件形式存储,无需安装SQL Server等服务,分发方便:

  • SQLite:轻量级关系型数据库,可搭配Entity Framework Core使用,支持SQL查询和索引,适合需要复杂数据检索的场景。
  • LiteDB:专为.NET设计的NoSQL文件数据库,API简洁类似MongoDB,支持LINQ查询,对字典、数组等复杂结构兼容性好,无需额外配置。
  • LiteDB示例:
using LiteDB;

// 保存数据
using (var db = new LiteDatabase("bars.db"))
{
    var collection = db.GetCollection<TitaniumBar>("titaniumBars");
    collection.Insert(Program.TitaniumBars);
}

// 读取数据
using (var db = new LiteDatabase("bars.db"))
{
    var collection = db.GetCollection<TitaniumBar>("titaniumBars");
    Program.TitaniumBars = collection.FindAll().ToList();
}

3. Protobuf(Protocol Buffers)

Google开发的高效序列化协议,体积远小于JSON/XML,读写速度快,支持跨语言。通过定义.proto文件描述数据结构,生成对应的.NET类即可完成序列化/反序列化,适合高性能或跨语言交互场景。

4. YAML序列化

YAML是人类可读的文本格式,比JSON更简洁,支持复杂嵌套结构。使用YamlDotNet库实现序列化,优点是可读性强,适合需要人工查看或编辑数据的场景;缺点是读写效率和体积不如二进制方案。

5. 自定义文本格式

根据自身数据结构设计专属文本格式(比如扩展CSV支持嵌套),手动实现读写逻辑。优点是格式完全可控,适合轻量化特定场景;缺点是需要自行处理解析和序列化,开发工作量大,不适合复杂结构。


内容的提问来源于stack exchange,提问作者Komaludim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 10:17:48