|
-
Sep 12th, 2008, 08:43 AM
#1
Thread Starter
Fanatic Member
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.
-
Sep 12th, 2008, 08:49 AM
#2
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.
-
Sep 12th, 2008, 09:01 AM
#3
Thread Starter
Fanatic Member
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.
-
Sep 12th, 2008, 10:37 AM
#4
Thread Starter
Fanatic Member
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 ?
-
Sep 12th, 2008, 07:56 PM
#5
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".
-
Sep 12th, 2008, 09:34 PM
#6
Re: XPath and Configuration
I didn't see anything about 2005, so perhaps some LINQ to XML?
vb.net Code:
Dim root As XElement = XElement.Load("c:\MyConfig.xml")
Dim connString = _
(From el In root.<connectionStrings>.<add> _
Select el.@connectionString).First()
MessageBox.Show(connString)
-
Sep 15th, 2008, 04:21 AM
#7
Thread Starter
Fanatic Member
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.
-
Sep 15th, 2008, 06:26 AM
#8
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|