本帖最后由 gidiyis 于 2010-02-03 15:36:52 编辑

解决方案 »

  1.   

    你像普通文件那样读写xml不就可以了吗?
      

  2.   

    不就是读XML,修改XML吗!去查查很多的!具体方法就不发了!
      

  3.   

    public string GetConfigValue(string path,string appKey)  
    {     
      XmlDocument xDoc = new XmlDocument();     
      try
       {
         xDoc.Load(path);     
        //xDoc.Load(System.Windows.Forms.Application.ExecutablePath+".config");
        XmlNode xNode;     
        XmlElement xElem;     
        xNode = xDoc.SelectSingleNode("//appSettings");    
        xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='"+appKey+"']");     
        if(xElem!=null)   
         return xElem.GetAttribute("value");     
        else     
         return   "";
       }
     catch(Exception)
       {
         return "";
       }
      

  4.   


     /// <summary>
            /// 修改配置文件
            /// </summary>
            /// <param name="connString"></param>
            public static void UpdateConfig(string p_strKey, string p_strValue)
            {
                try
                {
                    string m_strFullPath = "";
                    Assembly Asm = Assembly.GetExecutingAssembly();
                    XmlDocument xmlDoc = new XmlDocument();                m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + "YourApplication.exe.config";
                    xmlDoc.Load(m_strFullPath);                XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes;
                    foreach (XmlNode xn in nodeList)//遍历所有子节点
                    {
                        XmlElement xe = (XmlElement)xn;                    if (xe.GetAttribute("key").IndexOf(p_strKey) != -1)
                        {
                            xe.SetAttribute("value", p_strValue);
                        }
                    }
                    xmlDoc.Save(m_strFullPath);
                }
                catch (System.NullReferenceException NullEx)
                {
                    throw NullEx;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
      

  5.   

            
            /// <summary>
            /// 修改链接字符串
            /// </summary>
            /// <returns></returns>
            private bool UpdateConfig(string path, bool PartnerSystem)
            {
                System.IO.FileInfo FileInfo = new System.IO.FileInfo(dir + path);
                if (!FileInfo.Exists)
                {
                    throw new InstallException("Missing config file :" + this.Context.Parameters["targetdir"] + "web.config");
                }
                System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
                xmlDocument.Load(FileInfo.FullName);
                bool FoundIt = false;
                foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])
                {
                    if (Node.Name == "add")
                    {
                        if (Node.Attributes.GetNamedItem("name").Value == "Connection")
                        {
                            Node.Attributes.GetNamedItem("connectionString").Value = String.Format("server={0};database={1};uid={2};pwd={3};pooling=true;charset=gbk;", ServerName, DBName, AdminName, AdminPwd);
                            FoundIt = true;
                        }
                    }
                }
                if (PartnerSystem)
                {
                    foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["appSettings"])
                    {
                        if (Node.Name == "add")
                        {
                            if (Node.Attributes.GetNamedItem("key").Value == "PartnerSystem")
                            {
                                Node.Attributes.GetNamedItem("value").Value = this.Context.Parameters["partner"].ToString();
                                FoundIt = true;
                            }
                        }
                    }
                }
                if (!FoundIt)
                {
                    throw new InstallException("Error when writing the config file: web.config");
                }
                xmlDocument.Save(FileInfo.FullName);
                return FoundIt;
            }希望对你有帮助
      

  6.   

    添加app.config Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
    config.AppSettings.Settings["abc"].Value = abc;
      

  7.   

    XmlElement xe = (XmlElement)xn;4楼的 大侠,程序运行到这里报错,错误是:无法将类型为“System.Xml.XmlComment”的对象强制转换为类型“System.Xml.XmlElement”。
    我对操作XML文件不怎么会,
    我想把config里的每个节点都显示到窗体上相应的文本框里,实现这样的一个功能方法如何实现,还请帮帮忙!
      

  8.   

    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); 
    config.AppSettings.Settings["abc"].Value = abc;
    或者是用xml
      

  9.   


    解决了原来是 config里的注释,把注释去掉了都没有问题了