C# 导出/写入多维数组到文件(csv 或其他)

发布于 2024-12-09 14:44:39 字数 547 浏览 0 评论 0原文

您好,正在设计一个程序,我只是想要有关将 multiDim 数组写入文件的建议。 我正在使用 XNA 并有一个多维数组,其中包含 Vector3(x, y, z)。

有数十万甚至数百万个值,我希望能够将它们保存在文件中(保存游戏级别)。我对一个想法没有偏见,我只需要存储数据......就是这样!所有其他游戏数据(例如玩家统计数据等)我都在使用 XMLSerializer 及其工作奇迹。

现在我经常使用 xml 序列化器,并且了解到您无法导出 MultiDim 数组...太令人沮丧了(但我确信有一个很好的理由 - 希望如此)。我玩过Jagged,但运气不好。

使用 System.IO.File.WriteAllText 然后很快就意识到这仅适用于字符串...daahhh

基本上我认为我需要使用 BinaryWrite 方法,重写我自己的序列化器,甚至尝试运行 sql 服务器来托管大众数据...愚蠢的想法?请告诉我,你能指出我的写作方向吗?由于我主要有网络(php)背景,运行同步数据/关卡数据的服务器的想法对我很有吸引力......但可能不适用于此处。

谢谢你的一切,

马尔

Hi Designing a program and i just wanted advise on writing a multiDim array to a file.
I am using XNA and have a multidimension array with a Vector3(x, y, z)'s in it.

There are hundred thousand if not millions of values, and i want to be able to save them in a file (saving the game level). I have no bias to one idea, i just need to store data...thats it! All the other game data like player stats etc etc i am using XMLSerializer and its working wonders.

Now i was playing with xml serializer alot and have learn that you cannot export MultiDim Arrays... so frustrating (but i am sure there is a good reason why - hopefully). I played with Jagged's with no luck.

Used System.IO.File.WriteAllText then quickly relised that is only for string... daahhh

Basically i think i need to go down the BinaryWrite method, re-writing my own serializer, over even try running a sql server to host the masses of data... stupid idea? please tell me and can you point me in the write direction. As i primarily have a web (php) background the thought of running a server that syncs data / level data is attractive to me... but might not be applicable here.

Thanks for anything,

Mal

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

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

发布评论

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

评论(2

‖放下 2024-12-16 14:44:39

只要数组中的对象是可序列化的(IIRC Vector3 也是),您就可以使用内置的 .NET 序列化器来序列化批次。

void SerializeVector3Array(string filename, Vector3[,] array)
{
    BinaryFormatter bf = new BinaryFormatter();
    Stream s = File.Open(filename, FileMode.Create);
    bf.Serialize(s, array);
    s.Close();
}

Vector3[,] DeserializeVector3Array(string filename)
{
    Stream s = File.Open(filename, FileMode.Open);
    BinaryFormatter bf = new BinaryFormatter();
    Vector3[,] array = (Vector3[,])bf.Deserialize(s);
    s.Close();
    return array;
}

这应该是您所追求的内容的粗略模板。

You can just serialise the lot with the built-in .NET serialisers provided the objects in the array are serialisable (and IIRC Vector3s are).

void SerializeVector3Array(string filename, Vector3[,] array)
{
    BinaryFormatter bf = new BinaryFormatter();
    Stream s = File.Open(filename, FileMode.Create);
    bf.Serialize(s, array);
    s.Close();
}

Vector3[,] DeserializeVector3Array(string filename)
{
    Stream s = File.Open(filename, FileMode.Open);
    BinaryFormatter bf = new BinaryFormatter();
    Vector3[,] array = (Vector3[,])bf.Deserialize(s);
    s.Close();
    return array;
}

That should be a rough template of what you're after.

扎心 2024-12-16 14:44:39

为什么不尝试 Json 序列化呢? Json 比 XML 噪音更少,写入文件时占用的空间更少,尤其是在不缩进的情况下这样做。根据我的经验,它在处理数组、字典、日期和其他对象时没有问题。

我建议使用 JSON.NET ,如果没有,请查看 此线程

如果 Json.net 发现很难序列化具有许多私有 & 的库类;静态变量,那么编写 POCO 类并将库类基本属性映射到 POCO 并来回序列化和映射 POCO 就很简单了。

Why dont you try Json Serialization? Json has less noise than XML, occupies less space when written to file especially if you do so without indenting, etc. It does not have trouble with arrays, dictionaries, dates and other objects as far as my experience with it goes.

I recommend using JSON.NET and if not, then look at this thread

If Json.net finds it difficult to serialize a library class with many private & static variables, then it is trivial to write a POCO class and map the library class essential properties to your POCO and serialize and map the POCO back and forth.

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