c# - deserialize `Dictionary<string, Tuple<string, List<string>>>` from custom XML using XElement -
i have dictionary<string, tuple<string, list<string>>> email
in want write xml (serialize) , load xml dictionary.
i got write xml such:
public void writetoxml(string path) { var xelem = new xelement( "emailalerts", email.select(x => new xelement("email", new xattribute("h1", x.key), new xattribute("body", x.value.item1), new xattribute("ids", string.join(",", x.value.item2)))) ); xelem.save(path); }
but i'm stuck on loadxml takes xml path , loads dictionary inside email class
this have far:
public void loadxml(string path) { var xelem2 = xelement.parse(path); var demail = xelem2.descendants("email").todictionary(x => (string)x.attribute("h1"), (x => (string)x.attribute("body"), x => (string)x.attribute("body"))); }
background information xml should this
<emailalerts> <email h1='test1' body='this test' ids='1,2,3,4,5,10,11,15'/> </emailalerts>
try this:
var str = @"<emailalerts> <email h1='test1' body='this test' ids='1,2,3,4,5,10,11,15'/> </emailalerts>"; var xml = xelement.parse(str); var result = xml.descendants("email").todictionary( p => p.attribute("h1").value, p => new tuple<string, list<string>>( p.attribute("body").value, p.attribute("ids").value.split(',').tolist() ) );
Comments
Post a Comment