1

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.

4
  • What have tried so far, show your code to explain more clearly. Commented Feb 15, 2013 at 5:47
  • 2
    What do you mean: "it should not be updated in the XML file"? Commented Feb 15, 2013 at 5:49
  • 1
    Side note: one usually don't "send" values to a file, files are either read or written (opened/parsed/saved...) - are you sure you really mean "send"? Also please check if "it should not be updated in the XML" is correct statement... And adding some code you tried to work with XML (like create XmlDocument would go long way in making question better). Commented Feb 15, 2013 at 5:51
  • and y are you sending values to xml if you don't want to update the xml Commented Feb 15, 2013 at 5:51

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.