Question

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.

Was it helpful?

Solution

.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).

OTHER TIPS

A CSV file would be nice and compact.

XmlDocument

Don’t use XmlDocument unless you 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 you 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. You can then split on the first occurrence of “=” and read it into a Dictionary, a List, or some custom structure. It will be faster than XML and easier to maintain.

Spelling

I don’t think XML helps you 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 https://stackoverflow.com/questions/tagged/localization+.net

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top