views:

878

answers:

13

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.

+1  A: 

ini files?

[section]
key=value

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

Tom Anderson
Thanks, but no xml.Also ini is an inferior version of yaml. And I have to parse, validate etc... myself.
drozzy
No you don't: see the GetPrivateProfileXxx APIs. But you're right about their very limited capabilities.
itowlson
A: 

If your data isn't too complex or hierarchical, you could store it in a properties file format like this:

level.name=My Level
level.mapfile=Map.txt

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.

Andy White
Yes it can. Sorry no XML.Property files eh? Is there a parser for those?
drozzy
Easy enough to write one, if you don't get too fancy on the syntax. But I agree, hand-editing XML is something I don't really like to do.
Joey
A: 

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.

Thank you, I know how to google. I don't know how reliable that yaml library is. The first .NET library seems unstable, the 2nd one is in "progress".
drozzy
No fear, drozzy; use it anyway, fix the bugs as you go alone. C'mon, you know you want to...
I don't really want to branch of in that direction. I am looking for something ready made. I am really surprised by domination of the xml over all the microsoft developers. It's like a hive...
drozzy
+3  A: 

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)

Robert Gould
This. Have a data export process in the build process that parses the xml and generate a binary file that can be read straight in your game objects at runtime.
jfclavette
+1 Worked in the games industry for years too and this is exactly what we do. Don't diss XML, it may be verbose but it's far and away the best tool for the job
zebrabox
+2  A: 

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.

eglasius
Regarding .cs files, I forgot to mention that I want the ability to create (and save) new levels in game, through an editor.
drozzy
I see your point. You can still have an editor, but unless it does a full override the config won't be as clean after the edit i.e. you can : 1) load the existing config, and save additional actions done in the editor i.e. add update/remove/add to the config at the bottom of the methods 2) load the existing config, generate a new one on save - would erase any comments in the file but won't avoid duplication as it can generate just add lines.
eglasius
A: 

Since you don't want to use XML, I'd recommend creating your own file format that is text editable.

PROPERTIES
name = First Level
number = 1
END

MONSTERS
Barbarian
pos = (3, 6)
health = 2

Dragon
pos = (10, 10)
health = 8
END

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.

DeadHead
I think you're underestimating the level of effort involved in designing a domain-specific language and then building a parser for it by a factor of approximately one jillion.
Robert Rossney
No. I've built a number of parsers for my own formats I've made specifically for various programs. I've even built a simple parser for XML and I wouldn't call it that hard at all.
DeadHead
It depends on the complexity of the language you design.
Waylon Flinn
I agree with that.
DeadHead
Thanks, I think I might use that approach. But i am not sure still if I will use XML or not. It depends on how I solve the problem.
drozzy
A: 

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.

daz-fuller
+1  A: 

The whole idea of parsing html-like structure every time you need some data seems stupid.

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.

Robert Rossney
A: 

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.

Waylon Flinn
A: 

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.

John Saunders
+1  A: 

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.

orip
Cool, it looks more pleasant than XML. Is there some way to "validate" the input data? Thanks!
drozzy
There's an JSON Schema proposal (http://www.json.com/json-schema-proposal/), and Json.NET - my favorite .NET JSON parser/generator - supports it in the latest version (http://james.newtonking.com/archive/2009/01/04/json-net-3-5-beta-2-json-schema-validation.aspx). I've never used it, though.
orip
A: 

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! :-)

anonymous
A: 

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!

zebrabox