首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请问一个C#修改xml文档的有关问题

2013-04-20 
请教一个C#修改xml文档的问题?xml version1.0 encodingutf-8 ?configurationconfigSectionss

请教一个C#修改xml文档的问题


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="WebService测试程序.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <WebService测试程序.Properties.Settings>
            <setting name="WebService测试程序_WebReference1_Service1" serializeAs="String">
                <value>http://10.68.69.203:8081/TWS.asmx</value>
            </setting>
            <setting name="WebService测试程序_WebReference2_Service1" serializeAs="String">
                <value>http://10.68.69.203:8082/TWS.asmx</value>
            </setting>
        </WebService测试程序.Properties.Settings>
    </applicationSettings>
</configuration>


这就是xml文档的内容,是配置文件文件名为WebService测试程序.vshost.exe.config

我的程序可以获取到那个value的值但是我修改之后没有改变值不知道是什么问题请高手指导一下 谢谢

private void button1_Click(object sender, EventArgs e)
        {
            //string exeConfigFile = Process.GetCurrentProcess().MainModule.FileName + ".config";
            //XmlDocument doc = new XmlDocument();
            //doc.Load(exeConfigFile);
            //XmlNodeList list = doc.DocumentElement.GetElementsByTagName("applicationSettings");
            //foreach (XmlNode node in list)
            //{
            //    MessageBox.Show(node.ChildNodes[0].ChildNodes[0].InnerText);
            //    node.ChildNodes[0].ChildNodes[0].InnerText = "aa";
            //    MessageBox.Show(node.ChildNodes[0].ChildNodes[0].InnerText);
            //}

            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;


            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("setting");
            for (int i = 0; i < nodes.Count; i++)
            {
                ////获得将当前元素的key属性
                //XmlAttribute att = nodes[i].Attributes["key"];
                ////根据元素的第一个属性来判断当前的元素是不是目标元素
                //if (att.Value == strKey)
                //{
                //    //对目标元素中的第二个属性赋值
                //    att = nodes[i].Attributes["value"];
                //    att.Value = ConnenctionString;
                //    break;
                //}
                for (int j = 0; j < nodes[i].ChildNodes.Count; j++)
                {
                    if (i==0 && j==0)
                    {
                        nodes[i].ChildNodes[j].InnerText = "aa";
                    }
                }
            }
            //保存上面的修改
            doc.Save(strFileName);
            
        }


执行下来都没有问题 但是 内容我看了看 确实是没有修改不知道是什么问题。

热点排行