views:

102

answers:

3

Currently I just have a file like this that I manually parse into a Dictionary:

str1=Hello
str2=World

This is no longer practical for several reasons:

  • I need lists of strings that I can access by index
  • I want to split the key value pairs into groups

I consider using XML now because:

  • Good for key value pairs and groups
  • I could use classes and serialize them, eliminating spelling errors

But (assuming I use serialized classes):

  • I would have to create a lot of new classes to map the document structure
  • Dictionaries need to be cared for specially because the can't usually be serialized

Do you recommend this approach? Should I use the XmlDocument class instead? Is there some other format that has free libraries for .net?

I want to use this for localizing an application.

+5  A: 

.NET has pretty solid Resource-based support for localization, so you might look into that.

If you really want to invent your own resource storage format, I'd suggest you to look more closely at JSON as serialization format (there are plenty of .NET libraries to handle it) or at YAML (not sure if there are decent .NET parsers): they both are much simpler on the eye (if that's what you're after).

Anton Gogolev
How do you store lists in resources? Are there any issues with mono?
Not sure about lists. Why would you want to store them in resource files? Neither I know anything about Mono.
Anton Gogolev
The values should be numbered for use in lists and as enum value names.
Well, having Value1, Value2, ..., ValueN as keys, you can always for-loop over them and retrieve localized string with call to ResourceManager.GetString()
Anton Gogolev
The strings should still have a name. I'll probably go with a class with Dictionaries and serialize it to XML or JSON.
A: 

csv would be nice and compact

wefwfwefwe
How would you do the grouping?
key, value a, value b, value c...
wefwfwefwe
A: 

XmlDocument

Don’t use XmlDocument unless u absolutely need to. It is optimized for constructing xml not reading it. http://www.mattberther.com/2004/06/16/xmldocument-vs-xpathnavigator/ Use either an XmlReader or an XPathNavigator. If u are reading into classes consider doing a direct serialize with XMLSerializer.

Xml versus flat file

Only use XML if you are creating a hierarchy of objects. From your description a simple line by line read of a text file would suffice. U can then split on “=” and read it into a Dictionary or List or some custom structure. It will be faster than XML and easier to maintain.

Spelling

I don’t think XML helps u with spelling errors.

Localization

Should be done with resource files Have a look here http://www.codeproject.com/KB/dotnet/Localization.aspx or on SO http://stackoverflow.com/questions/tagged/localization+.net

Simon