I am building a c# Windows application to maintain a WEB application - special to create and update values in a web.config file. I have tried many suggestions here, but all of them are simply describing to read and write config files from inside the application. Part of the web.config:
-
<configuration>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true" targetFramework="4.0" />
<globalization culture="" enableClientBasedCulture="true" uiCulture="" />
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
</system.web>
<appSettings>
<add key="DocPath" value="~/serverdocs/docs" />
<add key="TextForLabel1" value="This is some text"/>
..
I tried to read the file - special all keys from appSettings -like
var xmlDoc = new XmlDocument();
xmlDoc.Load(FName); // name and path from web.config file
And then to read all nodes like :
foreach (XmlNodeList xmlnodes in xmlDoc.SelectNodes("configuration/appSettings"))
{
foreach ( XmlNode node in xmlnodes)
{
string MyKey = node.Name;
string MyVal = node.Value;
}
}
But there are always errors like The object of type "System.Xml.XmlDeclaration" cannot be converted to Type "System.Xml.XmlNodeList". Or the items are simply not found depending on how I write the select value. I have tried '//configuration' and 'configuration/appSettings' and others.
Sometimes I am probably blind reading my own code and detecting the error - sorry - any suggestion is welcome.