tags:

views:

581

answers:

9
+1  Q: 

File Parsing in C#

I am building a game in Visual Studio 2008 and in order to build levels, i will have two types of files (more than that actually but these two are the only ones important to my question) i need to parse. One will dictate the contents of a level and will look something like this:

LevelName = "Sample Level"

Object1Type = "CustomObject"

Object1File = "WideFloor_DF"

Object1Position = 600,600

Object2Type = "Circle"

Object2Position = 550, 500

Object2Mass = 5

etc.

The other will dictate the properties for custom objects, and look something like this:

Name = "Wide Floor"

Mass = 1

GeometryMap = "WideFloor_GM"

IsStatic = true

etc.

I'm not very familiar with regexes (read: i don't like them because the look too much like line noise to easily understand) and i was wondering if there is an easier method than using tons of regexes?

edit: i knew i forgot something! i really hate xml files, so i would really prefer not to use them (waaaaaaaay too verbose for me). edit 2: i like the format i've come up with and would prefer not to change.

+7  A: 

What about using XML files? You can read and write them easily using XMLReader and XMLWriter objects, and you won't have to deal with Regex at all.

<Level name="Sample Level">
   <Object type="CustomObject" file="WideFloor_DF" positionX="600" positionY="600" />
   <Object type="Circle" file="WideFloor_DF" positionX="500" positionY="500" mass="5" />
</Level>
Robert Harvey
Thanks for trying, and that way does look appealing, but i guess i have xmlphobia. Sorry!
RCIX
@RCIX, XML is a far more robust answer to this problem than your fragile regex solution. Get over your xmlphobia.
KingNestor
*gulp* i guess i will... thanks!
RCIX
if you still don't like XML:http://www.yaml.org/http://yaml-net-parser.sourceforge.net/
russau
+1  A: 

If you are going to have your files in human readable format anyway, why not use XML?

Tetraneutron
+1  A: 

If you have control over the file format you might try something with an existing parser, like XML or INI files, both of which have parsers in .NET.

If you're sold on the above format, you might use a tool like Lex to generate your parser for you.

Talljoe
+1  A: 

One alternate approach might be to define a class that has properties/collections to hold all of this configuration data, and make it [Serializable]. You can then populate out an example configuration, serialize it out to a file, and then you have a complete XML config file that you can edit by hand. When you're ready to use the configuration, just deserialize the XML file back into the config object in your app.

Andy White
A: 

well, if i understood your question you want a way to structure the contents of the files so that parsing will be so much easier for you, XML is a good choice for that, and XSD also for describing the content of your xml files, LINQ to XML will make it very good to read it from C#.

abdelkrimnet
A: 

If you use XML you can deserialise the files directly into an object so that you don't need to parse it at all.

Mark
+1  A: 

If you absolutely insist on using that file format, you can build an object that reads the file line by line, pulling things from the left side of the '=' and putting it into a Dictionary as the Key and everything to the right of the '=' as the Value.

However, I must echo the above answers that say to use an XML document along with an XMLDocument object. Have you looked into LINQ to XML?

jasonh
My problem with XML is that it is kinda hard to build by hand. *sigh* but there may not be any other way...
RCIX
Robert Harvey
A: 

Uh .... String.Split?

Josh Einstein
A: 

Why not binary serialize your objects.

the_ajp
I'm manually (read: building in an xml file) my levels, which will have no object data associated initially.
RCIX