网站配置文件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); }