Hi,
I've used this article to write Dictionary to XML :
This produce this XML file and I have a double node level ContentControls and Controls. Is it possible to avoid it ?
Thanks.
<?xml version="1.0" encoding="utf-8"?><OfficeContentControls xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Version major="1" minor="1" /><ContentControls><Controls><Control key="Expediteur.Nom" value="Mme XXXXXXXXXXXX Muriel" /><Control key="Expediteur.Adresse1" value="Lieudit Le Champ des Genêts" /><Control key="Expediteur.Adresse2" value=" " /><Control key="Expediteur.CodePostal" value="61360" /><Control key="Expediteur.Ville" value="Saint Jouin de Blavou" /><Control key="Expediteur.Pays" value="FRANCE" /></Controls></ContentControls></OfficeContentControls>
C# Code :
public class OfficeContentControls { public OfficeContentControls () { m_Dictionary = new Dictionary<string, string>(); m_Version = new XmlConfigurationVersion(1,1); } XmlConfigurationVersion m_Version; public XmlConfigurationVersion Version { get { return m_Version; } set { m_Version = value; } } Dictionary<string, string> m_Dictionary; [System.Xml.Serialization.XmlIgnore ()] public Dictionary<string, string> Controls { get { return this.m_Dictionary; } } [System.Xml.Serialization.XmlElement("ContentControls")] public XElement XmlDictionary { get { XElement root = new XElement("Controls"); return DictToXml (this.m_Dictionary, "Control", root); } set { this.m_Dictionary = XmlToDictionary("key", "value", value); } } public static Dictionary<string, string> XmlToDictionary (string key, string value, XElement baseElm) { Dictionary<string, string> dict = new Dictionary<string, string>(); foreach (XElement elm in baseElm.Elements()) { string dictKey = elm.Attribute(key).Value; string dictVal = elm.Attribute(value).Value; dict.Add(dictKey, dictVal); } return dict; } public static XElement DictToXml (Dictionary<string, string> inputDict, string valuesName, XElement root) { XElement outElm = root; Dictionary<string, string>.KeyCollection keys = inputDict.Keys; foreach (string key in keys) { XElement inner = new XElement(valuesName); inner.Add(new XAttribute("key", key)); inner.Add(new XAttribute("value", inputDict[key])); outElm.Add(inner); } return outElm; } }