C#导出/将多维数组写入文件(csv或其他格式) [英] C# export / write multidimension array to file (csv or whatever)
问题描述
您好,我正在设计一个程序,我只是想提供有关将multiDim数组写入文件的建议。
我正在使用XNA,并且其中带有Vector3(x,y,z)的多维数组。
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.
如果不算百万,就有十万个值,我希望能够将它们保存在文件中(保存游戏级别)。我对一个想法没有偏见,我只需要存储数据即可。所有其他游戏数据,例如玩家统计数据等,我都在使用XMLSerializer及其工作奇迹。
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.
现在我在大量使用xml序列化程序,并且了解到您无法导出MultiDim数组...太令人沮丧了(但我敢肯定有充分的理由-希望如此)。我没碰运气就玩过锯齿游戏。
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.
使用过的System.IO.File.WriteAllText然后很快就被删除了,只适用于字符串... daahhh
Used System.IO.File.WriteAllText then quickly relised that is only for string... daahhh
基本上,我认为我需要使用BinaryWrite方法,重新编写自己的序列化程序,甚至尝试运行sql服务器来托管大量数据...这是愚蠢的主意吗?请告诉我,您能指出我的写作方向吗?由于我主要具有网络(php)背景,因此想到运行一台同步数据/级别数据的服务器对我来说很有吸引力……但可能不适用于此。
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.
感谢任何东西,
Mal
推荐答案
您只需序列化即可如果数组中的对象是可序列化的(并且IIRC Vector3是),则内置了.NET序列化程序。
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.
这篇关于C#导出/将多维数组写入文件(csv或其他格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!