c# - Loading Xml File Into Section, Key and Value Collection -
we have settings file xml extending pluggable modules writing. want use our existing xml settings allow extension methods written on them. long story short if had xml file so:
<settings> <settingsone key1_1="value1" key1_2="value2" /> <settingstwo key2_1="value1" key2_2="value2" /> </settings>
how load collection of settingsentry settingsentry looked so:
public class settingsentry { public string section { get; set; } public string key { get; set; } public string value { get; set; } }
where section "settingsone", key "key1_1" , value "value1".
is possible or going down dark path?
edit:
ok suggestion of linq xml life save, trying xmlserializer! below have far, there way turn single select rather 2 have below:
var root = xelement.load(pathtoxml); var sections = el in root.elements() select el.name; list<settingsentry> settings = new list<settingsentry>(); foreach (var item in sections) { var attributes = el in root.elements(item).attributes() select new settingsentry() { section = item.localname, key = el.name.localname, value = el.value }; settings.addrange(attributes); } return settings;
edit 2:
this seems work.
var sections = el in root.elements() in root.elements(el.name).attributes() select new settingsentry() { section = el.name.localname, key = a.name.localname, value = a.value };
you can in 1 linq query :
var attributes = attribute in root.elements().attributes() select new settingsentry() { section = attribute.parent.name.localname, key = attribute.name.localname, value = attribute.value }; return attributes.tolist();
Comments
Post a Comment