Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

I have an utility that modified ASP.NET web.config using code like this:

XmlDocument document = new XmlDocument();
document.Load(sConfigFileName);
XmlNode nodeParent = document.SelectSingleNode("/configuration/system.web");

I found that this code doesn't work with VS 2005 (SelectSingleNode returns null) because configuration element has xmlns

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> .

I was able to delete xmlns attribute without any visible side effects to make SelectSingleNode("/configuration/system.web") working .

But it will be probably required to use new classes in Configuration namespace or use XmlNamespaceManager.

I think that Visual XPath source can be a sample for this.

Posted on Thursday, February 2, 2006 1:17 PM ASP.NET , .Net Framework | Back to top


Comments on this post: Parsing web.config with XmlDocument.SelectSingleNode

# re: Parsing web.config with XmlDocument.SelectSingleNode
Requesting Gravatar...
How do you remove "xmlns" from a xml node!?
Left by R. Neves on Mar 11, 2006 12:45 AM

# re: Parsing web.config with XmlDocument.SelectSingleNode
Requesting Gravatar...
I didn't delete xmlns attribute from xml node, but deleted it from web.config file using text editor.
Left by Michael Freidgeim on Mar 11, 2006 6:12 AM

# re: Parsing web.config with XmlDocument.SelectSingleNode
Requesting Gravatar...
No need to delete the xmlns just add a xmlNameSpaceManager.

XmlDocument document = new XmlDocument();
document.Load(sConfigFileName);
XmlNamespaceManager ns = new XmlNamespaceManager(document.NameTable);

ns.AddNamespace("x", "http://schemas.microsoft.com/.NetConfiguration/v2.0");

XmlNode myNode = document.SelectSingleNode("/x:configuration/x:system.web" ns);
Left by Brad Welborn on Mar 20, 2006 1:53 PM

# re: Parsing web.config with XmlDocument.SelectSingleNode
Requesting Gravatar...
Brad,
Your code will work fine with assumption that config file has the xmlns attribute.
The ideal code should check does xmlns attribute exist and add to namespace "http://schemas.microsoft.com/.NetConfiguration/????" only if required. This is what Visual XPath source code does.
Left by Michael Freidgeim on Mar 21, 2006 12:12 PM

# re: Parsing web.config with XmlDocument.SelectSingleNode
Requesting Gravatar...
there's aboslutely nothing about adding the /x: to the path in the VisualStudio 2005 help - you (and Google is my friend) have just saved me hours of hair-pulling! Many thanks indeed :)
Left by hoverfrog on Mar 13, 2007 2:51 AM

# re: Parsing web.config with XmlDocument.SelectSingleNode
Requesting Gravatar...
Ditto - I was writing a post-build cleanup script to go through copious web.config files and making sure the environment variables were correct for every web app on the box - this saved a LOT of time.
Left by John Scott on May 26, 2007 3:24 AM

# re: Parsing web.config with XmlDocument.SelectSingleNode
Requesting Gravatar...
Nice work Brad! The only improvement I would make to your code is the following:

Dim XMLDoc As New System.Xml.XmlDocument()
XMLDoc.Load(HttpContext.Current.Server.MapPath("Web.config"))

Dim XMLNsMgr As New System.Xml.XmlNamespaceManager(XMLDoc.NameTable)

' "wc" symbolizes Web.config but could be any string
' I dynamically obtain the XML Namespace from the Web.config and use it when creating the XML Namespace Manager
XMLNsMgr.AddNamespace("wc", XMLDoc.DocumentElement.Attributes("xmlns").Value)

Dim FormsAuthenticationTimeoutValue As Integer = CInt(XMLDoc.SelectSingleNode("/wc:configuration/wc:system.web/wc:authentication/wc:forms", XMLNsMgr).Attributes("timeout").Value)

P.S. Thanks for getting me started!
Left by Johnson4321 on Mar 13, 2009 4:06 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net