C# XML格式转换字典

public static Dictionary GetXmlStr(string xmlStr)   //获取 xml 字符串 转字典
        {
            Dictionary kv = new Dictionary();
            XmlDocument x = new XmlDocument();
            x.LoadXml(xmlStr); //传入xml字符串  
            //x.Load(); //传入 xml文件路径

            XmlNode rootNode = x.SelectSingleNode("/xmldata");
            if (rootNode != null)
            {
                XmlNodeList xmlNodes = rootNode.ChildNodes;
                foreach (XmlNode xmlNode in xmlNodes)
                {
                    if (!xmlNode.InnerText.Equals(""))
                    {
                        kv.Add(xmlNode.Name, xmlNode.InnerText);
                    }
                    else
                    {
                        kv.Clear();
                        break;
                    }
                    
                    
                }
            }

你可能感兴趣的:(笔记)