Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

[XML] [Serialization] Dictionary double node level

$
0
0

Hi,

I've used this article to write Dictionary to XML :

http://stackoverflow.com/questions/1799767/easy-way-to-convert-a-dictionarystring-string-to-xml-and-visa-versa

This produce this XML file and I have a double node level ContentControls and Controls. Is it possible to avoid it ?

Thanks.

XML Result:

<?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;
        }
    }


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>