读取xml文件(可执行文件根目录debug)

时间:2021-02-27 12:44:56

xml文件格式如下

1 <?xml version="1.0" encoding="utf-8" ?>
2 <root>
3   <appKey>abcdefg</appKey>
4   <appSecret>hijklmn</appSecret>
5   <serverUrl>opqrst</serverUrl>
6   <accessToken>uvwxyz</accessToken>
7 </root>

读取格式如下,我是放在构造函数里面

先声明几个私有变量接受

private string SERVER_URL;
private string accessToken;
private string appKey;
private string appSecret; 
 3             //判断可执行文件下有么有JD_API.xml文件,也就是debug目录下
 4        if (File.Exists(Application.StartupPath + "\\JD_API.xml"))
 5        {
 6            XmlDocument xml = new XmlDocument();
 7            xml.Load(Application.StartupPath+"\\JD_API.xml");//加载xml文件
 8            appKey = xml.SelectSingleNode("root/appKey").InnerText;//读取xml节点
 9            appSecret = xml.SelectSingleNode("root/appSecret").InnerText;
10            SERVER_URL = xml.SelectSingleNode("root/serverUrl").InnerText;
11            accessToken = xml.SelectSingleNode("root/accessToken").InnerText;
12       }