如 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;