Results 1 to 8 of 8

Thread: XPath and Configuration

  1. #1

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

    XPath and Configuration

    VS 2005
    Hi All,

    I have a small config file :
    Code:
    <?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>
    And would like to edit both the "ProgramService" connection string service and the value of "loggingLevel" in the appsettings using XPath.

    Can someone point me in the right direction ?
    Last edited by venerable bede; Sep 15th, 2008 at 04:21 AM.

    Parksie

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

    Re: XPath and Configuration

    Don't use XPath. The Framework has an inbuilt mechanism for editing the config file. Follow the Protected Configuration link in my signature for an example.
    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

  3. #3

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

    Re: XPath and Configuration

    Thanks for that but I dont think I can use that approach. That is if you are using the "System.Configuration;" namespace.

    You see the config file is required by a reflected assembly which is why I would like to use XPath.
    Last edited by venerable bede; Sep 12th, 2008 at 09:35 AM.

    Parksie

  4. #4

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

    Re: XPath and Configuration

    Actually I dont really need to use XPath.

    All I need is to extract the value of the two nodes. I have tried to get data using
    Code:
    XmlNodeList elemList = doc.GetElementsByTagName("SupplierId");
    but for the life of me it returns a count of 0.

    What am I doing wrong ?

    Parksie

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

    Re: XPath and Configuration

    I don't see any SupplierId tag in that XML code. Note that within the appSettings section all the tags are named "add". If you were looking for that loggingLevel bit then you'd have to get a tag whose name was "add" and had an attribute named "key" with a value of "loggingLevel".
    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

  6. #6
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: XPath and Configuration

    I didn't see anything about 2005, so perhaps some LINQ to XML?
    vb.net Code:
    1. Dim root As XElement = XElement.Load("c:\MyConfig.xml")
    2.  
    3. Dim connString = _
    4.     (From el In root.<connectionStrings>.<add> _
    5.     Select el.@connectionString).First()
    6.  
    7. MessageBox.Show(connString)

  7. #7

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

    Re: XPath and Configuration

    I'm a bit screwed here.

    I use the following to open the doc:
    Code:
     Dim doc As New XmlDocument()
     doc.Load("Myconfig.exe.config")
    And I use this to get the tags with "add" in them :
    Code:
    Dim elemList As XmlNodeList = doc.GetElementsByTagName("add")
    This returns the appropriate number of nodes.
    Now how do I find the "<add key="loggingLevel" value="4" />" and how do I update the value ?

    This is crazy. I'm reflecting assemblies but can't figure out simple XML
    I'm an embaressment !!
    Last edited by venerable bede; Sep 15th, 2008 at 05:13 AM.

    Parksie

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

    Re: XPath and Configuration

    The tag name is "add" and it has attributes named "key" and "value". You need to get the "add" tag with a "key" attribute whose value is "loggingLevel" and then set the value of its "value" attribute.
    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

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