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

保存与读取是同一种方法的不同表示;

posted on 2018-06-01 11:28  卡丘  阅读(5709)  评论(0编辑  收藏  举报