By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

XPath and web.config

P: n/a
I must be completely losing my mind.

I have some code that writes to config files. It works great with
app.config files, but fails miserably with web.config files. For the life
of me, I cannot figure out what is going on here.

I have taken it all the way back to just selecting the configuration node
(top level node), and it fails!

How can this line fail (returns null)??
node = xmlDoc.SelectSingleNode("/configuration").

When this line returns "configuration"?
xmlDoc.ChildNodes[2].Name

How this fail??

node = xmlDoc.SelectSingleNode(xmlDoc.ChildNodes[2].Name)

Jun 6 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Dear J.Marsch

The code snppet you post in newsgroup looks fine.
I'm not sure what do you mean by "Fails". Does the SelectSingleNode method
return Null object?

If true, I would like to suggest you make sure the web.config file doesn't
specify namespace, Such as
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

Otherwise the xpath "/configuration" couldn't find any node.

You may either remove the default namespace, or use an XmlNamespaceManager
with SelectSingleNode.

Hope this helps, please let me know if you still meet any issue. I'm glad
to assist.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 7 '07 #2

P: n/a
I must be completely losing my mind.

I have some code that writes to config files. It works great with
app.config files, but fails miserably with web.config files. For the life
of me, I cannot figure out what is going on here.

I have taken it all the way back to just selecting the configuration node
(top level node), and it fails!

How can this line fail (returns null)??
node = xmlDoc.SelectSingleNode("/configuration").

When this line returns "configuration"?
xmlDoc.ChildNodes[2].Name

How this fail??

node = xmlDoc.SelectSingleNode(xmlDoc.ChildNodes[2].Name)

This may be your problem:

In your first example, because you're using XPath you will be relying on the namespace environment declared in the xml config file, which might look like:
&lt:configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"&gt;

XPath can't cope with default namespaces, so you can either (1) use '/configuration' in your xpath and remove the xmlns declaration, or (2) change the xpath to '/def:configuration' and modify the xmlns declaration to:

xmlns:def=http://schemas.microsoft.com/.NetConfiguration/v2.0

Be aware that if your config files are auto-generated using a tool, like Visual Studio, the namespace declaration may be modified the next time the configs are changed, and then the XPath won't work again.

Sorry if your problem has nothing to do with namespaces, but default namespaces are quite a common issue with xpath, so worth mentioning.

Phil Fearon
http://www.sketchpath.com
BizTalk Utilities - Frustration free BizTalk Adapters
http://www.topxml.com/biztalkutilities
Jun 7 '07 #3

P: n/a
Thank you, Phil.

I stumbled on to this very same thing thing last night. Comparing several
configs that were working with those that weren't, I found the ones that did
not work had a namespace defined at the configuration node. When I removed
the namespace declaration, everything worked. I will have to keep an eye
out to see if the IDE sneaks the namespace declaration back in there. I
might move to a node by node iteration instead of using XPath. It is more
cumbersome, but in this case it should be more reliable.

Thanks again for your help.

-- Jeremy
"Philip Fearon" <pg*****@googlemail.comwrote in message
news:b2**********************************@text.gig anews.com...
>
>I must be completely losing my mind.

I have some code that writes to config files. It works great with
app.config files, but fails miserably with web.config files. For the
life
of me, I cannot figure out what is going on here.

I have taken it all the way back to just selecting the configuration node
(top level node), and it fails!

How can this line fail (returns null)??
node = xmlDoc.SelectSingleNode("/configuration").

When this line returns "configuration"?
xmlDoc.ChildNodes[2].Name

How this fail??

node = xmlDoc.SelectSingleNode(xmlDoc.ChildNodes[2].Name)


This may be your problem:

In your first example, because you're using XPath you will be relying on
the namespace environment declared in the xml config file, which might
look like:
&lt:configuration
xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"&gt;

XPath can't cope with default namespaces, so you can either (1) use
'/configuration' in your xpath and remove the xmlns declaration, or (2)
change the xpath to '/def:configuration' and modify the xmlns declaration
to:

xmlns:def=http://schemas.microsoft.com/.NetConfiguration/v2.0

Be aware that if your config files are auto-generated using a tool, like
Visual Studio, the namespace declaration may be modified the next time the
configs are changed, and then the XPath won't work again.

Sorry if your problem has nothing to do with namespaces, but default
namespaces are quite a common issue with xpath, so worth mentioning.

Phil Fearon
http://www.sketchpath.com
BizTalk Utilities - Frustration free BizTalk Adapters
http://www.topxml.com/biztalkutilities

Jun 7 '07 #4

P: n/a
Hello Jeremy,

By the way, you may specify Namespace to execute Xpath query in this
scenario.

System.Xml.XmlNamespaceManager xmlnsManager = new
System.Xml.XmlNamespaceManager(xmldoc.NameTable);
xmlnsManager.AddNamespace("nc",
"http://schemas.microsoft.com/.NetConfiguration/v2.0");
System.Xml.XmlNodeList List =
xmldoc.SelectNodes("/nc:configuration",xmlnsManager);
....

http://support.microsoft.com/kb/318545
[HOW TO: Specify Namespaces When You Use an XmlDocument to Execute XPath
Queries in Visual C# .NET]

Hope this helps. Have a great weekend. Thanks.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 8 '07 #5

P: n/a
Thank you.

Since it seems that some of the web.configs have the namespace separator and
some do not, it looks as though I have 2 tasks in front of me:

1. Determine in what cases the namespace specifier will be in the config
file (for my own edification)
2. Find a way to determine whether the namespace is declared, and add to
the query if necessary.

These are both very doable. Thank you very much.

"WenYuan Wang [MSFT]" <v-******@online.microsoft.comwrote in message
news:Eu**************@TK2MSFTNGHUB02.phx.gbl...
Hello Jeremy,

By the way, you may specify Namespace to execute Xpath query in this
scenario.

System.Xml.XmlNamespaceManager xmlnsManager = new
System.Xml.XmlNamespaceManager(xmldoc.NameTable);
xmlnsManager.AddNamespace("nc",
"http://schemas.microsoft.com/.NetConfiguration/v2.0");
System.Xml.XmlNodeList List =
xmldoc.SelectNodes("/nc:configuration",xmlnsManager);
...

http://support.microsoft.com/kb/318545
[HOW TO: Specify Namespaces When You Use an XmlDocument to Execute XPath
Queries in Visual C# .NET]

Hope this helps. Have a great weekend. Thanks.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Jun 15 '07 #6

P: n/a

Thanks for your reply, Jeremy.
You are welcome.:)
If there is any further issue you have to met, please also feel free to let
us know.
I'm standing by.

Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 18 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.