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 Petzi at 6/18/2007 5:27 PM
Gravatar
string xml = document.DocumentElement.OuterXml.Replace("xmlns=", "hacked_xmlns=");
document.LoadXml(xml);

is maybe somewhat ... chainsaw-style, but avoids the need of using that annoying "x:" prefixes in every XPath.

Your comment:



 (will not be displayed)


 
 
 
Please add 7 and 7 and type the answer here:
 

Live Comment Preview:

 
«October»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678