Results 1 to 4 of 4

Thread: Read a node in config xml

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    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?

    Parksie

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Read a node in config xml

    You could do

    Code:
    Dim myValue As String = System.Configuration.ConfigurationManager.AppSettings("STAMP")
    Please mark you thread resolved using the Thread Tools as shown

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width