The Last Configuration Section Handler.. Revisited

If you need to store a little bit of state-- in your configuration file, or on disk-- nothing is faster than some quick and dirty serialization. Or as I like to call it, stringization.

This is a companion discussion topic for the original blog entry at:

“The Language of the Gods, VB.NET

Oh no you di’int.


Here is an intensive course of serialization with Ruby. Note that the XML serialization could be further improved. :slight_smile:

require 'cl/xmlserial’
require ‘yaml’

a = %w(aaaa bbbbbb ccccccccccc) # Array


s = Marshal.dump(a)
a_copy = Marshal.load(s)


xml = a.to_xml.to_s
a_from_xml = Array.from_xml(


yaml = a.to_yaml
a_from_yaml = YAML.load(yaml)

class Person
include XmlSerialization
attr_accessor :name

o = = ‘Joe’


s = Marshal.dump(o)
o_copy = Marshal.load(s)


xml = o.to_xml.to_s
o_from_xml = Person.from_xml(


yaml = o.to_yaml
o_from_yaml = YAML.load(yaml)

VB.NET has something called a root namespace, which is different from the C# default namespace. Whenever a root namespace is defined (in project properties) it’s added behind your back.

I have no idea why anyone would want this, or why it is that way. It’s strange to have something added invisibly in a language which force us to write code about the size of a book just to get a read-only property.

Tish and pish, how about in the language of the proletariat, PHP:

$string=serialize( $my_array );



I still think those public fields just look wrong wrong wroooong…

If you try to serialize objects this way, you’re going to run into security problems. The app.config file is stored in the same folder as the executable which is usually somewhere in the Progrm Files folder. Standard users do not have write access to Progrm Files.

Here’s a good… uh, arguable (dodging bullets?) metric to fan some flames in the CS v. VB war:
5K 10K
I challenge your Language of the Gods with my Language of the Verbose.

I think all ideas which produce discussion are great.

You must also consider that the class might have overriden the root element name for the class (XmlRoot).

Therefore, add this code:

// Foo becomes Foo type=“MyClass.Foo"
Type t = o.GetType();
string name = t.Name;
object[] attributes = t.GetCustomAttributes(typeof(XmlRootAttribute), false);
if (attributes != null attributes.Length 0)
XmlRootAttribute xmlRoot = attributes[0] as XmlRootAttribute;
name = xmlRoot.ElementName;
s = Regex.Replace(s, “(” + name + “)()”, “$1 type=”” + t.FullName + “”$2");

You have to be crazy to use regex’s for Xml Serialization… here it is in four lines:

public string Serialize(object obj)
using(StringWriter sw = new MemoryStream())
XmlSerializer xs = new XmlSerializer(obj.GetType());
xs.Serialize(sw, obj);
return sw.ToString();

If you want to change the name of fields or objects in the XML you simply apply attributes to them:

public class MyStuff
public int i;

public string s;


The reason why PHP serializes more simply is because XML is only one type of serialization available to .NET you can also (for instance) serialize to a byte[] or to JSON by using the above code with simply using different formatters. Also you should check out Windows Communication Foundation and how to make a DataContract… very cool stuff.

Still brilliant; I can’t count how many times I’ve used this. Also, I believe you may use:

New XmlSerializer(t, New XmlRootAttribute(section.Name))

…to remove the dependancy of the element name to class name (even if root is renamed or redeclared or attribute sprinkled - thanx 4 the hint Michael P) .

(You may also consider encoding the xml values to UTF8 first)

Thanks Jeff, Craig, Michael Jef N.

VB.NET may not be the language of the Gods, but it’s certainly the language of love. There, I said it.

Yeah, I reference the root namespace every time I have to summon up an embedded resource. Big deal.

VB.NET is the Language Of Love!!!

Love. 3

Allow me to reiterate. Assembly (and, to a lesser extent, C and its’ variants) is the language of love for the computer. It affords every convience for the lowest level, for the compiler, and makes people bend to its’ will. Similarly, the LOL (Language of Love, VB.NET), bends over backwards to make itself readable to humans. This means that is is easier for LOL (VB.NET) programmers to write code that is more self-documenting. To write code which resembles the syntax of the English lanuage. VB.NET is the language of love for the developer!

The true virtue of any language these days is not speed or brevity. It’s readability and maintainability. VB.NET leverages the same framework as C#, but in a way that allows the developer to name things in such a way that his code is self explanatory. And that’s worth a thousand semicolons and two thousand curly braces.

And that’s love for the person typing in the code. And the friend that’s reading it.


As of .NET 2.0, there’s no need to use this approach for configuration handlers - just use the new attribute-based (or object model) approach.

The key attribute is ConfigurationPropertyAttribute and its MSDN docs illustrate nicely how to use them:

This new method even allows validation of your config properties.

Hehe, but I still use the old method in any 1.1 apps :slight_smile:

.NET 2.0’s config classes/attributes are great for basic persistence. Not so great for deep-nested data structures or for producing truly human-readable XML, and then there’s the security issue.

Using proxy classes as in this post is generally a good way to tackle the issue because they are easy to serialize. It’s also pretty much the only way to transport data over a Web Service in a form readable by non-.NET apps. One could also write a small persistence class library, if one were so inclined; 2.0’s Generics make life a lot easier in that respect.

As for the VB.NET sub-discussion, simply because the syntax is closer to plain English than C# does not make it any more readable or maintainable. Verbosity may facilitate understanding for some but is a distraction to others. And I’m not a C# evangelist - my primary language is Delphi, which is similar to VB in a lot of ways, but I “love” all of those languages equally because I am “fluent” in all of them, and I think that if people would bother to learn other tools beyond 10-minute tutorials then they might be a little less stuck-up about the tool of their choice. Ruby and Python, for example, give orders of magnitude more “love” to developers than any of the .NET languages, in some areas at least.