【发布时间】:2013-09-09 16:28:01
【问题描述】:
我想使用我自己的 xml 代码(不是 linq to xml)读取 App.config 文件的 <appSettings> 部分:
这是我的 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>
我的 c# 代码:
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 有 3 种模式,但我没有设法访问每一种。
我还想阅读system.net 部分。
【问题讨论】:
-
你不喜欢 ConfigurationManager.AppSettings 吗?
-
是的,但我更喜欢用这种方式来学习阅读 xml。
-
@senyorToni 我认为现在学习使用 LINQ 阅读 xml 会更好。十年前学习 XmlDocument 很有趣。 AccessDenied 也是正确的 - 最好将 ConfigurationManager 用于此类任务
标签: c# .net xml system.xml