views:

188

answers:

1

Does anyone know if there is a way to convert the following string into an object?

"width: 100px; height: 20px; border: solid 1px black;"

As you notice, this is a standard CSS property. I know it would be fairly trivial to split on ';' and do the work myself, but looking at some other languages it seems they have native support to do this...

I've been playing around with the JavaScriptSerializer class, but it seems it wants native JSON format.

Thoughts on the easiest, cleanest and most robust way to get this data into a more structured format?

A: 

You can get the keys and values into a dictionary (C# code):

Dictionary<string, string> items =
   data
   .Split(';')
   .Select(s => s.Trim())
   .Where(s => s.Length > 0)
   .ToDictionary(
      s => s.Substring(0, s.IndexOf(':')).Trim(),
      s => s.Substring(s.IndexOf(':') + 1).Trim()
   );
Guffa
Yep, I ended up writing something like this but was dismayed to find that there wasn't something already in .NET thanks
dmose