I want to read the <appSettings>
section of the App.config
file using my own xml code (not linq to xml):
Here's my app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="driver" value="C:/"/>
<add key="daysToExpire" value="0"/>
<add key="Interval" value="5000"/>
</appSettings>
<system.net>
<mailSettings >
<smtp>
<network enableSsl="false"
port="25"
host="smtp.gmail.com"
defaultCredentials="false"
/>
</smtp>
</mailSettings>
</system.net>
My c# code:
XmlDocument doc = new XmlDocument();
doc.Load(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.xml")
);
XmlNodeList appSettings = doc.SelectNodes("/configuration/appSettings/add");
Driver = appSettings[0].Attributes[0].Value;
Interval = Convert.ToInt16(appSettings[2].Value);
DaysToExpire = Convert.ToInt16(appSettings[1].Value);
appSettings
has 3 modes but I didn't manage to access each one.
I also want to read the system.net
section.
If you just want to read the app.config settings, use the configuration manager. If you are trying to learn how to use XmlDocument, you have a few problems.
1) You really should not depend upon the order of nodes within an XML document in the general case, you have more stable code if you parse depending upon the XML content. This is not your actual problem.
This is actually an XmlElement node. It contains 2 XmlAttribute nodes. It does not have a Value. Actually, en XmlElement node never have a Value (the .Value property will always be null). A "value" in an XmlElement will be the .InnerText (or .InnerXml) property.
If you look at your code, you correctly read the driver value as an Attribute, but you did not do so for the other two nodes. You still have the problem of retrieving the attribute based on the index instead of the attribute name. Using the name is a more stable parsing technique.