0

Dictionary

Dictionary<XElement, XElement> _XParents = new Dictionary<XElement, XElement>();

My dictionary looks like:

key:<node>, value:<parentNode>

I want to convert this dictionary to XML file

XML should look like:

<parentNode>
    <node/>
</parentNode>

1 Answers1

1

Try following :

            XElement root = new XElement("Root");

            foreach (XElement _XParent in _XParents.Keys)
            {
                root.Add(_XParent);
            }

Your dictionary key should be a string so code should look like this

            Dictionary<string, XElement> _XParents = new Dictionary<string, XElement>();

            XElement root = new XElement("Root");

            foreach (string _XParent in _XParents.Keys)
            {
                root.Add(new XElement(_XParent, _XParents[_XParent]));
            }
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • I can not change the dictionary data type I have to use dictionary – Vishal Joshi Feb 03 '21 at 20:52
  • It will not work. The keys will be different for each XElement and will include all the children. To get the string of the key you can use LocalName.Name. – jdweng Feb 03 '21 at 21:51