I have a Dictionary<string, Tuple<string, List<string>>> email
in which i want to write to XML (Serialize) and also load from XML to Dictionary.
I got the write to XML as 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 where it takes the XML path and loads it into the dictionary inside this Email Class
This is what i have so 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 My XML should be something like this
<emailAlerts>
<email h1='Test1' body='This is a test' ids='1,2,3,4,5,10,11,15'/>
</emailAlerts>
You can try this way :
path
parameter contains path to XML file, then you should've usedXElement.Load(path)
instead ofXElement.Parse(path)
.Tuple.Create()
to constructTuple
instance is often shorter thannew Tuple()
style