473,508 Members | 2,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XPath and web.config

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
6 7274
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
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3469
by: Michael Fork | last post by:
Note: I pasted the code the attachments as plain text after the message (I wasn't able to post it with an attachment...) Attached are the XSL and XML files that I am having problems with. I am...
3
1406
by: Shawn | last post by:
Hi. I have an XML file that looks like this: <?xml version="1.0" encoding="utf-16"?> <Transfer> <Config xmlns="http://www.mysite.com/Transfer/"> <site>NY</site> </Config> </Transfer> I'm...
2
5081
by: news.microsoft.com | last post by:
Hi After conducting a several searches, I wasn't sure where to post this question. So my apologiesbefore hand for double posting this and possibly posting to the wrong forum(s). Okay, I have...
7
2033
by: jon cosby | last post by:
This code shows all values for the config node. I'm trying to retrieve the value for systrayenabled for attrib "jon". Something wrong? sXmlPath = Application.StartupPath.ToString() +...
1
3249
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file...
1
2575
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file...
0
789
by: Benny Ng | last post by:
Dear All, Modify1("SqlServer", "xxxxxxxxxxxxxx"); The error message is program can't find the XML segment <add name="SqlServer" , But it seems should be runs properly. What's wrong with the...
2
1831
by: =?Utf-8?B?U2JhdGNodQ==?= | last post by:
How will i read connection strings in all web.config files in one particualr machine?
0
2041
by: Curtin1060 | last post by:
Hi, I have a simple exe.config issue where I'd like to swap out the config file with NAnt/XMLPOKE. I just can't figure out the correct xpath to get to the "file=" attribute. I want to swap in...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.