1

this is my xml file...

-->

<!--Daily Genarated File Path-->
<add key="DailyFilName" value="C:\DailySummary.xls"/>
<!--Weekly Genarated File Path-->
<add key="WeeklyFilName" value="C:\WeeklySummary.xls"/>
<!--Log File Path-->
<add key="LogFilName" value="c:\\TranmittalsLog.txt"/>

i need to edit my DailyFilName by c#. Using Key i need to change the value.

1
  • is this App.config file? Commented Nov 23, 2010 at 9:13

4 Answers 4

3

Well depending on the type of File there are a number of options you can use.

If it is a standard XML file then there are built in .NET classes you can use such as XmlReader, XmlWriter and XPathNavigator. Examples avaliable on MSDN.

If it is an app.config file then you can use the Configuration namespace to work directly with the file without the need to read/write the Xml manually. Check out the ConfigurationManager class on MSDN for some examples.

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

Comments

1

[NOTE: if you are trying to manipulate appSettings section in app.config or web.config file, use of ConfigurationManager is recommended.]

You can do something like this :

    private void SetValue(String key, String value)
    {
        XDocument doc = XDocument.Load("...");
        XElement element = doc.Descendants("add").Where(d => d.Attribute("key") != null && d.Attribute("key").Value == key).First();
        element.Attribute("value").Value = value;
    }

Usage

SetValue("DailyFilName", "...");

Comments

1

I think you want this if you are working with a app.config file

ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = "C:\\App.config"};

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

config.AppSettings.Settings["SettingKey1"].Value = "newValue";
config.Save();

Comments

0
private void SetValue(string xmlFilePath, string key, string value)
{
   try
   {
      XDocument doc = XDocument.Load(xmlFilePath);
      XElement element = doc.Descendants("add").Where(d => d.Attribute("key") != null && d.Attribute("key").Value == key).First();
      element.Attribute("value").Value = value;
      doc.Save(xmlFilePath);
    }
    catch (Exception ex)
    {
      MessageBox.Show(ex.Message);
    }
}

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.