System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
string path = AppDomain.CurrentDomain.BaseDirectory + "XXXXXXX.exe.config";
xDoc.Load(path);
XmlNodeList nodelist = xDoc.DocumentElement.ChildNodes;
foreach (XmlElement xel in nodelist)
{
if (xel.Name.ToLower() == "appsettings")
{
XmlNodeList node = xel.ChildNodes;
if (node.Count > 0)
{
foreach (XmlElement element in node)
{
if (element.Attributes["key"].Value == "url" && textURL.Text != "")
{
element.Attributes["value"].Value = textURL.Text;
xDoc.Save(path);
}
if (element.Attributes["key"].Value == "way" && textDataBase.Text != "")
{
element.Attributes["value"].Value = textDataBase.Text;
xDoc.Save(path);
}
if (element.Attributes["key"].Value == "time" && textTime.Text != "")
{
element.Attributes["value"].Value = textTime.Text.ToString();
xDoc.Save(path);
}
}
}
}
}
}winform中修改配置文件 同理可以是xml等其他
最新推荐文章于 2025-11-30 11:43:28 发布
本文介绍了一种使用C#编程语言动态读取并修改XML配置文件中特定节点属性的方法。通过遍历XML文档的节点,可以查找名为'appsettings'的元素,并更新其中带有'key'属性的子元素的'value'值。
1509

被折叠的 条评论
为什么被折叠?



