将数据存储在 C# 源文件与 XML 等中...?
这是一个 C# 问题。
我只是想知道是否有人尝试将其数据(例如配置文件)存储在 *.cs 文件中,而不是 XML 中?
我真的很讨厌XML。 每次需要一些数据时解析类似 html 的结构的整个想法似乎很愚蠢。
是否有 XML 的轻量级替代方案(如 YAML)? 就我而言,我实际上需要存储(游戏)关卡的一些数据,其中有很多不同的对象、地形类型等......在平面文件中,以便我稍后可以读取它。
我想我可以序列化我的 Level 类,但我希望数据文件可以在任何文本编辑器中编辑。
欢迎任何帮助,谢谢!
PS:关于 .cs 文件,我忘记提及我希望能够通过编辑器在游戏中创建(和保存)新关卡。
This is a C# question.
I was just wandering if anyone tried storing their data (like config files for example), in the *.cs file, instead of XML?
I really hate XML. The whole idea of parsing html-like structure every time you need some data seems stupid.
Are there lightweight alternatives (like YAML) to XML?
In my case I actually need to store some data for a (game) Level, where there are a lot of different objects, terrain types, etc... in a flat file, so I can read it later.
I guess I could serialize my Level class, but I want the data-file to be EDITABLE in any text editor for example.
Any help would be welcome, thanks!
PS: Regarding .cs files, I forgot to mention that I want the ability to create (and save) new levels in game, through an editor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我在游戏行业工作多年,我们使用 XML 是因为它有最好的工具支持。 Yaml 或 Ini 文件用于配置,但游戏数据存储在 XML 中。 当然,我们通常拥有将 XML 转换为二进制代码的工具,用于发布游戏,但在开发和自定义游戏工具开发过程中,XML 简直太糟糕了。
有时 XML 是满足您需求的正确选择,不要仅仅因为技术在某些领域不是最好的就对它们产生偏见(例如 Ajax(传输)与数据定义)
I've been working in the game industry for years, and we use XML because it's got the best tool support. Yaml or Ini files are used for configs, but game data is stored in XML. Of course we normally have tools that convert XML into binary code, for shipping games, but during development and custom game tools development XML simply rocks.
Sometimes XML is the right hammer for your needs, don't place prejudice on technologies just because they aren't the best in some fields (say Ajax(transport) vs Data Definition)
由于您不想使用 XML,因此我建议您创建自己的可文本编辑的文件格式。
等等,无论你需要什么。
自己创建一个解析器非常简单。 几乎只是逐行读取文件。
在第一行,您看到标题是“PROPERTIES”,因此您可以读取属性,直到读取 END 行。 对于您拥有的任何其他标头也是如此。 有很多方法可以改变这一点,因此您可以按照您想要的方式创建语法。
Since you don't want to use XML, I'd recommend creating your own file format that is text editable.
and so on so forth for whatever else you need.
Creating a parser yourself is really straight forward. Pretty much just read in the file line by line.
At the first line, you see the title is PROPERTIES so you then read in properties until you read in the END line. Same thing for any other headers you'd have. There are many ways you can vary this, so you can make the syntax however you want it to be.
为什么不(xml)?
我没有看到不使用 XML 的情况,特别是考虑到您想要在文件中存储不同类型的信息。
也就是说,对于 .cs 文件方法,我建议构建应用程序代码,以便加载初始化所需数据的代码,而不是在文件中查找数据。 它为您提供了一个清晰的模式(编译+加载),因为您现在正在加载初始化类,而不是在文件中查找数据。 添加尽可能多的内容以使配置尽可能简单。
Why not (xml)?
I don't see a case for not using XML, specially considering you want to store different types of information in the file.
That said, for the .cs files approach I suggest structuring your application code so that you are loading code that initializes the data you need, instead of looking for data in the files. It gives you a clear pattern (compile + load), as you are now loading an initialization class, instead of looking data into a file. Add as much as you can to make the config in there as simple as possible.
ini 文件?
详细阅读并思考您的问题后,您会发现 XML 在游戏行业中是非常标准的,因为它具有结构化、支持儿童的功能,并且可以由任何文本编辑器进行手动编辑。
我想到的一些使用 XML 文件的游戏:
魔兽世界
孤岛危机
命令与 征服3
ini files?
After reading your question a bit more and thinking about it, XML is very standard in the gaming industry as it structured, supports children, as well as being hand editable by any text editor.
A few games that come to mind that use XML files:
World of Warcraft
Crysis
Command & Conquer 3
如果您不打算使用 XML 或 YAML,则必须编写自己的解析器和验证器,现在您已经进入了愚蠢统治的领域。 XML 很烦人,但如果没有操作它的工具生态系统,情况会更糟。
If you're not going to use XML or YAML, you're going to have to write your own parser and validator, and now you've entered the land where stupid reigns. XML's annoying as all get-out, but doing without the ecosystem of tools for manipulating it is worse.
我们喜欢使用 JSON 作为配置文件 - 它的用途非常广泛。
它易于手动编辑、易于解析和分析。 生成(包括强大的 .NET 库 - MS 和第 3 方),并且非常强大 - 具有良好基本数据类型的 XML。
We like JSON for config files - it's surprisingly versatile.
It's easy to hand-edit, easy to parse & generate (including strong libraries for .NET - both MS and 3rd party), and is very powerful - sort of XML with good basic datatypes.
如果您以前听说过,不知道,但您应该查看一下这个 YAML 内容。 它非常酷,有
自己的主页
和一切。Donno if you've heard of it before, but you should check out this YAML thing. It's totally cool, with
its own homepage
and everything.如果您的数据不太复杂或分层,您可以将其存储为如下属性文件格式:
等。
另一方面,XML 在 C# 中通过序列化(ISerialized 或 DataContract)确实很容易使用,您不需要不必编写任何解析代码,框架会为您完成这一切。 在文本编辑器中编辑 XML 并没有那么糟糕!
如果您愿意在 XML 上做出一些妥协,您可以使用 NameValueCollection 来保存您的配置项。 然后,您可以使用内置的 .NET 序列化来序列化它。 该文件将是 XML,但它将采用非常简单、易于编辑的格式,并且您不必担心任何手动类型转换/验证。
If your data isn't too complex or hierarchical, you could store it in a properties file format like this:
etc.
On the other hand, XML is really easy to work with in C# with serialization (ISerializable or DataContract), you don't have to write any parsing code, the framework does it all for you. Editing XML in a text editor can't be that bad!
If you're willing to compromise on XML a little, you could use a NameValueCollection to hold your configuration items. You can then serialize this using the built-in .NET serialization. The file will be XML, but it will be in a really simple, easy-to-edit format, and you won't have to worry about any manual type conversions/validation.
即使 XML 被认为相当重,通常也是在文件中存储数据的最佳选择,而 LINQ for XML 确实使读取和写入数据变得更加容易。
如果您的数据变得相当复杂,另一个选择是使用 SQLite 之类的东西,我以前使用过它,对于单用户访问来说,它确实非常快速且易于使用。
XML even thought quite heavy is often the best choice for storing data in a file, and LINQ for XML does make reading and writing that data a lot easier.
Another option if your data gets quite complex is to use something like SQLite, I've used this before and for single-user access it is really quite fast and easy to use.
听起来您想要一种可以在文本编辑器中编辑的二进制格式。 不幸的是(直到每个人都熟练使用十六进制编辑器)速度和编辑方便性之间存在不可避免的权衡。
由于二进制格式基本上是对状态进行编码的最小字节集的非常密集的包装,因此它会快速且易于解析。 另一方面,XML 格式则很容易编辑。 一种可能的解决方案是创建两者并构建一个在它们之间进行转换的简单程序。 然后,您的高级用户将使用此程序将您的关卡转换为 XML,对其进行编辑,然后再将其转换回来。
如果您最终在这种情况下使用 XML,我建议您使用多个文件(也许每个对象一个)。 听起来你计划拥有一个复杂的世界。 把它切碎一点是有道理的。
It sounds like you want a binary format that can be edited in a text editor. Unfortunately (until everyone is fluent with a hex editor) there's an inescapable trade-off between speed and ease of editing.
Since a binary format is basically a very dense packing of the smallest set of bytes that encode your state, it would be fast and easy to parse. An XML format, on the other hand, would be easy to edit. One possible solution is that you create both and build a simple program that converts between them. Your power users would then use this program to convert your levels to XML, edit them and then convert them back.
If you do end up using XML in this scenario, I'd recommend you use more than one file Perhaps one per object. It sounds like you plan on having a complex world. It makes sense to chop it up a little.
如果您使用 .NET 配置系统,它会为您进行 XML 解析。 请参阅如何:使用 ConfigurationSection 创建自定义配置部分。 尽管它位于 ASP.NET 部分,但它适用于所有 .NET 代码。
If you use the .NET Configuration system, it does the XML parsing for you. See How to: Create Custom Configuration Sections Using ConfigurationSection. Even though it's in an ASP.NET section, it's applicable to all .NET code.
从技术上讲,您不需要解析 XML,只需创建一个数据模型,然后序列化到 XML 或从 XML 反序列化即可。 在 Visual Studio 中为数据模型构建编辑器也非常简单,方法是使用 TreeView 或 ListView 显示数据模型中的每个项目/对象,并使用 PropertyEditor 编辑所选对象的属性。 使用 OpenFileDialog 和 SaveFileDialog 让用户选择 XML 文件和 XmlSerializer 来加载和保存数据模型。 我向你保证真的很无痛! :-)
Technically u dont need to parse XML, just create a data model and then serialize into and deserialize from XML. It is also very simple to build an editor in Visual Studio for the data model by having a TreeView or ListView displaying each item/object in the data model and use the PropertyEditor to edit the properties of the selected object. Use OpenFileDialog and SaveFileDialog to let the user select the XML file and XmlSerializer to load and save ur data model. really painless I assure u! :-)
我在游戏行业工作,我们几乎专门使用 XML 来完成您所描述的操作。
原因如下:
1. 很好理解并且是一个标准
2. 在库方面有很好的支持
3.它可以被验证 - 这在创建数据编辑器时至关重要
4.它是结构化的
5. 它是人类可读的,因此易于调试
事实上,您说您讨厌仅仅为了获得一个值而必须对其进行拖曳,这一事实向我表明您应该使用众多 xml 解析器之一。 相信我,它既简单又有效。
人们使用它有充分的理由。 是的,它确实有缺点,即冗长等,但相信我,优点远远超过缺点!
I work in the games industry and we do use XML pretty much exclusively to do what you describe.
Here's why:
1. It's well understood and is a standard
2. It has excellent support in terms of libraries
3. It can be validated - this is essential when creating data editors
4. It is structured
5. It is human readable and hence easily debuggable
The fact that you say you hate having to trawl it just to get a value suggest to me that you should be using one of the many xml parsers out there. Trust me it's easy and works
People use it for good reasons. Yes it does have it's downsides i.e verbosity etc but believe me the advantages far outway the disadvantages!