繁体   English   中英

如何在运行时使用 C# 将值发送到 XML 文件

[英]how to send values to XML file in runtime using C#

我想使用用户在文本框中给出的值作为 XMl 文件中 TestMode 的值。 XML 文件如下所示。

<appSettings>
    <add key="SaveWindowItemsMap" value="true"/>
    <add key="TestMode" value=""/>
</appSettings>

该值将在运行时由用户(在文本框中)给出,不应在 XML 文件中更新。

你能试试这个吗:

以下是修改应用程序设置值的代码示例:

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

foreach (XmlElement element in xmlDoc.DocumentElement)
{
    if (element.Name.Equals("appSettings"))
    {
        foreach (XmlNode node in element.ChildNodes)
        {
            if (node.Attributes[0].Value.Equals("SaveWindowItemsMap"))
            {
                node.Attributes[1].Value = "New Value";
            }
        }
    }
}

xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

ConfigurationManager.RefreshSection("appSettings");

我假设您要更新“SaveWindowItemsMap”值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM