C#为配置文件添加结点

private   void  addnewnode(  string  nodekey, string  nodevalue)
        
{
            
try
            
{
                System.Xml.XmlDocument xmlDoc 
= new System.Xml.XmlDocument();
                xmlDoc.Load(sInstallPath
+@"web.config");
                System.Xml.XmlNode node;
                XmlNode fanode
=xmlDoc.SelectSingleNode("//appSettings");
                node 
= xmlDoc.SelectSingleNode(string.Format("//appSettings/add[@key='{0}']",nodekey));
                
if(node==null)
                
{

                    XmlElement connode
=xmlDoc.CreateElement("add");
                
                    
//connode.Name="add";
                    connode.SetAttribute("key",nodekey);
                    connode.SetAttribute(
"value",nodevalue);
                    
//connode.Attributes["key"].Value=nodekey;
                    
//connode.Attributes["value"].Value=nodevalue;
                
                    fanode.AppendChild(connode);
                    xmlDoc.Save(sInstallPath
+"web.config");

                }

            }

            
catch(Exception er)
            
{
                MessageBox.Show(er.ToString());
            }

        }

你可能感兴趣的:(C#为配置文件添加结点)