FAQ FAQ  Forum Search   Register Register  Login Login

Problem with SelectSinglenode because of xmlns

 Post Reply Post Reply
Author
  Topic Search Topic Search  Topic Options Topic Options
howard View Drop Down
I'm new here
I'm new here


Joined: 25 Aug 2009
Online Status: Offline
Posts: 3
  Quote howard Quote  Post ReplyReply bullet Topic: Problem with SelectSinglenode because of xmlns
    Posted: 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
 
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.
 
 
Back to Top
jvierra View Drop Down
MVP
MVP


Joined: 31 Aug 2006
Location: United States
Online Status: Offline
Posts: 6846
  Quote jvierra Quote  Post ReplyReply bullet 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
Back to Top
jvierra View Drop Down
MVP
MVP


Joined: 31 Aug 2006
Location: United States
Online Status: Offline
Posts: 6846
  Quote jvierra Quote  Post ReplyReply bullet Posted: 25 Aug 2009 at 16:48
Ok - NO problem as I suspected.  No need to delete anything.
 
Given this web.config file:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    <system.web>
        <membership>
            <providers>
                <add name="WebAdminMembershipProvider" type="System.Web.Administration.WebAdminMembershipProvider" />
            </providers>
        </membership>
        <httpModules>
            <add name="WebAdminModule" type="System.Web.Administration.WebAdminModule"/>
        </httpModules>
        <authentication mode="Windows"/>
        <authorization>
            <deny users="?"/>
        </authorization>
        <identity impersonate="true"/>
       <trust level="Full"/>
       <pages validateRequest="true"/>
       <globalization uiCulture="auto:en-US" />
    </system.web>
</configuration>
 
This will enable queries to succeed:
 

[xml]$xml=gc web.config      
$mgr=new-object System.Xml.XmlNamespaceManager($xml.Psbase.NameTable)
$mgr.AddNamespace("gr",$xml.configuration.xmlns)
$xml.SelectNodes("//gr:system.web",$mgr)
 
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
Back to Top
jvierra View Drop Down
MVP
MVP


Joined: 31 Aug 2006
Location: United States
Online Status: Offline
Posts: 6846
  Quote jvierra Quote  Post ReplyReply bullet Posted: 25 Aug 2009 at 17:26
Her eis a slightly upated version that uses teh correct method of getting the namespace.
 

[xml]$xml=gc web.config      
$mgr=new-object System.Xml.XmlNamespaceManager($xml.Psbase.NameTable)
$mgr.AddNamespace("gr",$xml.configuration.psbase.NamespaceURI)
$xml.SelectNodes("//gr:system.web",$mgr)

 
OR
$xml.SelectSingleNode("/*/gr:system.web",$mgr)


Edited by jvierra - 25 Aug 2009 at 17:28
Back to Top
howard View Drop Down
I'm new here
I'm new here


Joined: 25 Aug 2009
Online Status: Offline
Posts: 3
  Quote howard Quote  Post ReplyReply bullet Posted: 26 Aug 2009 at 06:02
Thanks a lot jvierra.  This works great. 
Back to Top
jvierra View Drop Down
MVP
MVP


Joined: 31 Aug 2006
Location: United States
Online Status: Offline
Posts: 6846
  Quote jvierra Quote  Post ReplyReply bullet 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
Back to Top
howard View Drop Down
I'm new here
I'm new here


Joined: 25 Aug 2009
Online Status: Offline
Posts: 3
  Quote howard Quote  Post ReplyReply bullet 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. 
Back to Top
jvierra View Drop Down
MVP
MVP


Joined: 31 Aug 2006
Location: United States
Online Status: Offline
Posts: 6846
  Quote jvierra Quote  Post ReplyReply bullet Posted: 26 Aug 2009 at 09:17
See the following KB:
 
I suggest that if you are not getting any errors you probably want to leave the line in the config file as other things may have become dependent on it.  Use the PoSH workaround for safety.
The next time the developers update teh site they should remove the namespace reference and retest the site.
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down