Read a node in config xml
I have the following in my confid settings:
Code:
<configuration>
<appSettings>
<add key="STAMP" value="999" />
</appSettings>
</configuration>
If I want to alter the node 'STAMP' I do the following:
Code:
XmlElement elementStamp = (XmlElement)doc.SelectSingleNode("/configuration/appSettings/add[@key='STAMP']");
elementStamp.SetAttribute("value", "999");
What I want to know, and I know this is silly, but how do I read it?
Re: Read a node in config xml
You could do
Code:
Dim myValue As String = System.Configuration.ConfigurationManager.AppSettings("STAMP")
Re: Read a node in config xml
You should be using the ConfigurationManager class to access the config file to write too. If you follow the CodeBank link in my signature you can see how it's done in my Protected Configuration thread. I don't have a C# version of that code but the principles are exactly the same.
Re: Read a node in config xml
I used to do this with the 1.1 framework, I think there is another class for this now but never had any reason to use it. However at least this worked:
Code:
The configuration file:
<!--Configuration File - Darren Rattansingh -->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="nameval" value="192.122.0.5"/>
</appSettings>
</configuration>
C# code:
using System.Configuration;
// this will output the value of the key.
MessageBox.show(ConfigurationSettings.AppSettings["nameval"].ToString());
But as i said, I think this way has been replaced by another class. So you could do some research if necessary. The last time I checked however, you could still manipulate xml files with the 3.5 framework this way.