简体   繁体   中英

how to send values to XML file in runtime using C#

I want to use the value given by the user in the textbox as value for TestMode in the XMl file. The XML file looks like below.

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

The value is to be passed as given by the user(in text box) in runtime and it should not be updated in the XML file.

Can you try this :

Here is the code sample to modify the application settings value:

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");

I am assuming you want to update "SaveWindowItemsMap" value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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