xml转化为Dictionary

public SortedDictionary<string, object> FromXml(string xml)
    {
        SortedDictionary<string, object> m_values = new SortedDictionary<string, object>();
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml);
        XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点<xml>
        XmlNodeList nodes = xmlNode.ChildNodes;
        foreach (XmlNode xn in nodes)
        {
            XmlElement xe = (XmlElement)xn;
            m_values[xe.Name] = xe.InnerText;//获取xml的键值对到WxPayData内部的数据中
        }
        return m_values;
    }

 

Dictionary to Element:

Dictionary<string, string> dict = new Dictionary<string,string>();
XElement el = new XElement("root",
    dict.Select(kv => new XElement(kv.Key, kv.Value)));
Element to Dictionary:

XElement rootElement = XElement.Parse("<root><key>value</key></root>");
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach(var el in rootElement.Elements())
{
   dict.Add(el.Name.LocalName, el.Value);
}

  

装载:https://www.cnblogs.com/xsj1989/p/9884703.html

posted @ 2021-07-14 16:55  LiuzzBK  阅读(134)  评论(0编辑  收藏  举报