C# 讀取配置文件方法


如 xml中寫:

<?xml version="1.0" encoding="utf-8" ?>
<config>
  <serv_ip>192.168.0.105</serv_ip>
  <database>test</database>
  <userid>sa</userid>
  <password>123456</password>
</config>

  

那么代碼:

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("config.xml");
            XmlNode root = xmlDoc.SelectSingleNode("config");//指向根節點
            XmlNode xn = root.SelectSingleNode("serv_ip");//指向根節點下的serv_ip節點
            string ip = xn.InnerText;//讀出里面的值 注意讀取的是string 需要類型轉換的話自己做
            XmlNode database = root.SelectSingleNode("database");
            string data = database.InnerText;
            XmlNode userid = root.SelectSingleNode("userid");
            string user = userid.InnerText;
            XmlNode password = root.SelectSingleNode("password");
            string pwd = password.InnerText;

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM