1. 程式人生 > >xml文件保存與讀取(最基礎的方式)

xml文件保存與讀取(最基礎的方式)

clas sin pad bim add winform ima 文件 ID

一、從xml文件讀取數據到Winform:

1、調用 using System.Xml;

2、XmlDocument xmlDoc = new XmlDocument();

3、xmlDoc.Load(Application.StartupPath + "\\ParaConfig.xml");

4、XmlElement xmlRoot = xmlDoc.DocumentElement;
5、XmlNode login = xmlRoot.ChildNodes[0];
6、 this.txb1.Text = login.Attributes["1"].Value;
this.txb2.Text = login.Attributes["2"].Value;
XmlNode Setup = xmlRoot.ChildNodes[1];
this.cmb1.Text = Setup.Attributes["3"].Value;

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Application.StartupPath + "\\ParaConfig.xml");
            XmlElement xmlRoot = xmlDoc.DocumentElement;
            XmlNode login = xmlRoot.ChildNodes[0];
            this.txbIPAddress.Text = login.Attributes["IP"].Value;
            
this.txbUserName.Text = login.Attributes["Name"].Value; this.txbPassword.Text = login.Attributes["Password"].Value; XmlNode Setup = xmlRoot.ChildNodes[1]; this.cmbImageOrien.Text = Setup.Attributes["ImageOrien"].Value;

二、將Winform中的數據保存到xml文檔中

            XmlDocument xmlDoc = new
XmlDocument(); xmlDoc.Load(Application.StartupPath + "\\ParaConfig.xml"); XmlElement xmlRoot = xmlDoc.DocumentElement; XmlNode login = xmlRoot.ChildNodes[0]; login.Attributes["IP"].Value = this.txbIPAddress.Text; login.Attributes["Name"].Value = this.txbUserName.Text; login.Attributes["Password"].Value = this.txbPassword.Text; XmlNode Setup = xmlRoot.ChildNodes[1]; Setup.Attributes["ImageOrien"].Value = this.cmbImageOrien.Text;
//保存上面的修改  
xmlDoc.Save(Application.StartupPath + "\\ParaConfig.xml");

保存與讀取是同一種方法的不同表示;

xml文件保存與讀取(最基礎的方式)