Failed to edit web.config [modified]
-
Hello, My question may be very dumb :-( but I couldn't change my web.config using the the following code: XmlDocument doc = new XmlDocument(); doc.XmlResolver = null; doc.Load(@"C:\Web.config"); XmlNode node = doc.SelectSingleNode("/configuration/appSettings"); I don't know why it always returns null. Do you know what I stupidly did wrong? Thanks, Johnny Here is my file
-
Hello, My question may be very dumb :-( but I couldn't change my web.config using the the following code: XmlDocument doc = new XmlDocument(); doc.XmlResolver = null; doc.Load(@"C:\Web.config"); XmlNode node = doc.SelectSingleNode("/configuration/appSettings"); I don't know why it always returns null. Do you know what I stupidly did wrong? Thanks, Johnny Here is my file
If the XPath parameter passed to the SelectSingleNode() method does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace (and that is your actual case), you must add a prefix and namespace URI to the XmlNamespaceManager; otherwise, you do not get your node selected ;) So, replace your line:
XmlNode node = doc.SelectSingleNode("/configuration/appSettings");
with the following piece of code:XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); nsmgr.AddNamespace"pfx", "http://schemas.microsoft.com/.NetConfiguration/v2.0"); XmlNode node = doc.SelectSingleNode("//pfx:appSettings", nsmgr);
SkyWalker