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


一、從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"); 

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


免責聲明!

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



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