C#操作XML文档

网站配置文件Web.config是一xml文件,在对其进行配置时,可采用对一般xml文件进行操作的方式。

  XmlDocument xmldoc = new XmlDocument();
            string filename = Server.MapPath("..//") + "Web.config";
            xmldoc.Load(filename);
            XmlNode root = xmldoc.SelectSingleNode("configuration");
            XmlNode conNode = root.SelectSingleNode("connectionStrings");
            XmlNodeList conNodeList = conNode.ChildNodes;
            foreach (XmlNode node in conNodeList)
            {
                XmlElement element = (XmlElement)node;

                if (element.GetAttribute("providerName") == "System.Data.OleDb")
                {

                    element.SetAttribute("connectionString", sb.ToString());

                    element.SetAttribute("name", "connectionString");
                }
                xmldoc.Save(filename);
            }
View Code
原文地址:https://www.cnblogs.com/wangzhenghua/p/3084289.html