加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#读取XML文件数据和把数据保存至xml的方法

(2012-03-23 13:31:00)
标签:

杂谈

分类: c#


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

//需要添加的

using System.Xml;

using System.IO;

 

namespace xml

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        #region 加载窗体,加载数据

        private void Form1_Load(object sender, EventArgs e)

        {

            getFromXml();

        }

        #endregion 

 

        //变量声明

        string username;

        string password;

        string path = @"config.xml";

 

        //保存设置

        private void button1_Click(object sender, EventArgs e)

        {

            username = textBox1.Text;

            password = textBox2.Text;

            saveToXml(username,password);

            MessageBox.Show("保存成功");

        }

        #region 把数据保存至xml文件

        /// <summary>

        /// 保存至xml文件

        /// </summary>

        /// <param name="username">账号</param>

        /// <param name="password">密码</param>

        private void saveToXml(string username,string password) 

        {

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(path);

            XmlNode node; 

            node = xmlDoc.SelectSingleNode("config/username");

            if (node == null)

            {

                XmlElement n = xmlDoc.CreateElement("username");

                n.InnerText = username;

                xmlDoc.SelectSingleNode("config").AppendChild(n);

            }

            else

            {

                node.InnerText = username;

            }

            node = xmlDoc.SelectSingleNode("config/password");

            if (node == null)

            {

                XmlElement n = xmlDoc.CreateElement("password");

                n.InnerText = password;

                xmlDoc.SelectSingleNode("config").AppendChild(n);

            }

            else

            {

                node.InnerText = password;

            }

            xmlDoc.Save(path);

 

        }

        #endregion

 

        #region 从xml获得数据,并加载

        private void getFromXml()

        {

            //获得数据

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(path);

            XmlNode node;

            node = xmlDoc.SelectSingleNode("config/username");

            username = node.InnerText;

            node = xmlDoc.SelectSingleNode("config/password");

            password = node.InnerText;

            //加载数据

            textBox1.Text=username;

            textBox2.Text=password;

 

        }

        #endregion

 

    }

}

=======================

<?xml version="1.0" encoding="utf-8"?>

<config>

  <username>king</username>

  <password>123456</password>

</config>

 

如果config.xml格式正确

会提示

缺少根元素

更改一致就可以了

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有