Easy way to convert a Dictionary to xml and vice versa

前端 未结 6 1624
北海茫月
北海茫月 2020-11-30 22:13

Wondering if there is a fast way, maybe with linq?, to convert a Dictionary into a XML document. And a way to convert the xml back to a di

6条回答
  •  渐次进展
    2020-11-30 23:12

    Did something like this for an IDictionary

    XElement root = new XElement("root");
    
    foreach (var pair in _dict)
    {
        XElement cElement = new XElement("parent", pair.Value);
        cElement.SetAttributeValue("id", pair.Key);
        el.Add(cElement);
    }
    

    That produced the following XML:

    
      0
      1
      2
      3
    
    

提交回复
热议问题