editing a config file

B

bede

I have to write a small front end exe which will allow installers to
update a config file. The config file is not part of the project so I
cannot use the sonfiguration namespace but instead update it using
XML.

The config is as follows :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="ProgramService" connectionString="http://SERVERIP/
WebServices/myservice.asmx" />
</connectionStrings>
<appSettings>
<add key="loggingLevel" value="4" />
</appSettings>
</configuration>

I would like to edit both the "ProgramService" connection string
service and the value of "loggingLevel" in the appsettings but have no
idea where to start.
My XML is pretty poor.
Any ideas ?

Thanks In Advance
 
M

Marc Gravell

Something like:

XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlElement el
(XmlElement)doc.SelectSingleNode("/configuration/connectionStrings/add[@name='ProgramService']");
if (el != null)
{ // found
el.SetAttribute("connectionString", "foo");
}
el =
(XmlElement)doc.SelectSingleNode("/configuration/appSettings/add[@key='loggingLevel']");
if (el != null)
{ // found
el.SetAttribute("value", "bar");
}
doc.Save(path);

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top