
Problem with SelectSinglenode because of xmlns |
Post Reply
|
| Author | ||
howard
I'm new here
Joined: 25 Aug 2009 Online Status: Offline Posts: 3 |
Quote Reply
Topic: Problem with SelectSinglenode because of xmlnsPosted: 25 Aug 2009 at 14:47 |
|
|
I am trying to use powershell to update xml nodes in my web.config.
If the configuration element looks like below with xmlns it does not work
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
If I remove the xmlns attribute, then it works fine. I would rather not have to remove that attribute from all of our web.config files.
I want to be able to pass in parameters to the Powershell script, so I wrote it this way.
Param($config_path,$node,$new_value)
# Get the content of the config file and cast it to XML
$xml = [xml](get-content $config_path) #get node and set the new value for configSource
#//client[@configSource] sample node $xml.SelectSingleNode("$node").configSource = $new_value # Save it
$xml.Save($config_path) Is there any possible way I can use Powershell to do the same thing without having to remove the xmlns attribute.
Here's an article about why SelectSingleNode does not work with xmlns, but I do not know how I can do what I need with Powershell.
|
||
![]() |
||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6770 |
Quote Reply
Posted: 25 Aug 2009 at 15:00 |
|
|
I don't believe that is true at all. We do it all the time.
Look here:
The document you refered is talking about C issues and may not be relevant to PowerShell.
The namespace can be directly referenced in teh XPath or through a manager. Either way you don't need to remove the namespace from the config file.
Can you determine what tool is generating these decorated files? I do not get this even with Visual Studio 2008.
Edited by jvierra - 25 Aug 2009 at 15:07 |
||
![]() |
||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6770 |
Quote Reply
Posted: 25 Aug 2009 at 16:48 |
|
|
Ok - NO problem as I suspected. No need to delete anything.
Given this web.config file:
This will enable queries to succeed:
It's that simple. The manager just allows us to "alias" the default namespace. In PoSH the NameSpaceURI is not being used so we just pull the xmlns node value in and use that.
We could also have run an XSL transform that specifed removal of the namespace but it would stay removed when saved without using a second transform before saving.
You need the namespace for proper functioning of IIS. IIS reads this and knows that it is to process teh web.config using the semantics for ASP.NET 2.0. REmoval could cause evaluation errors and potentail security issues. Of course the system version of web.config would override much of the content.
Edited by jvierra - 25 Aug 2009 at 16:52 |
||
![]() |
||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6770 |
Quote Reply
Posted: 25 Aug 2009 at 17:26 |
|
|
Her eis a slightly upated version that uses teh correct method of getting the namespace.
Edited by jvierra - 25 Aug 2009 at 17:28 |
||
![]() |
||
howard
I'm new here
Joined: 25 Aug 2009 Online Status: Offline Posts: 3 |
Quote Reply
Posted: 26 Aug 2009 at 06:02 |
|
|
Thanks a lot jvierra. This works great.
|
||
![]() |
||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6770 |
Quote Reply
Posted: 26 Aug 2009 at 08:49 |
|
|
The better method would be to determine what the correct namespace alias is. All we are doing is overriding what has been declared in the assigned namespace. I bet there is a default declaration such as "msft" or nttwo or somethong similar. Knowing this would eliminate the need to alias.
Of course Microsoft may just be casting everything into the namespace with no alias.
I did a bit of further investigation and found that it is safe to remove this line from the file. It is apparently an issue with Visual Studio Bta code that places it in the file to begin with. This is apparently unintended and unnecessary for the released v2 ASP.NET code.
Edited by jvierra - 26 Aug 2009 at 09:15 |
||
![]() |
||
howard
I'm new here
Joined: 25 Aug 2009 Online Status: Offline Posts: 3 |
Quote Reply
Posted: 26 Aug 2009 at 09:12 |
|
|
thanks for the clarification on the issue jvierra. I did notice that our recent application do not have the xmlns line, but all our older ones did.
|
||
![]() |
||
jvierra
MVP
Joined: 31 Aug 2006 Location: United States Online Status: Offline Posts: 6770 |
Quote Reply
Posted: 26 Aug 2009 at 09:17 |
|
![]() |
||
Post Reply
|
| Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |