Parsing web.config with XmlDocument.SelectSingleNode

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 @ Thursday, February 02, 2006 1:17 PM

Print

Comments on this entry:

# re: Parsing web.config with XmlDocument.SelectSingleNode

Left by R. Neves at 3/11/2006 12:45 AM
Gravatar
How do you remove "xmlns" from a xml node!?

# re: Parsing web.config with XmlDocument.SelectSingleNode

Left by Michael Freidgeim at 3/11/2006 6:12 AM
Gravatar
I didn't delete xmlns attribute from xml node, but deleted it from web.config file using text editor.

# re: Parsing web.config with XmlDocument.SelectSingleNode

Left by Brad Welborn at 3/20/2006 1:53 PM
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);

# re: Parsing web.config with XmlDocument.SelectSingleNode

Left by Michael Freidgeim at 3/21/2006 12:12 PM
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.

# re: Parsing web.config with XmlDocument.SelectSingleNode

Left by hoverfrog at 3/13/2007 2:51 AM
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 :)

# re: Parsing web.config with XmlDocument.SelectSingleNode

Left by John Scott at 5/26/2007 3:24 AM
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.

# re: Parsing web.config with XmlDocument.SelectSingleNode

Left by Johnson4321 at 3/13/2009 4:06 PM
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!

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345