Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deserialize XML collection to dictionary in .NET?

suppose I have the following XML: <appSettings> <add key="key1" value="value1" /> <add key="key2" value="value2" /> <add key="key3" value="value3" /> <add key="key4" value="value4" /> </appSettings>

and I would like to transform it to an object like:

AppSettings["key1"] = "value1"; AppSettings["key2"] = "value2"; AppSettings["key3"] = "value3";

and so forth …

I've looked it up on the internet and haven't found yet something useful. Can someone help me out on this ?

like image 367
Yossi Avatar asked Sep 21 '11 09:09

Yossi


1 Answers

Easy.

var xd = XDocument.Parse(xml);

var AppSettings = xd.Root.Elements("add")
    .ToDictionary(
        xe => xe.Attribute("key").Value,
        xe => xe.Attribute("value").Value);
like image 104
Enigmativity Avatar answered Sep 19 '22 08:09

Enigmativity