1. 程式人生 > >C#讀取XML檔案資料和把資料儲存至xml的方法

C#讀取XML檔案資料和把資料儲存至xml的方法

原文在百度知道中,來源於多個網友。

新浪微博:http://blog.sina.com.cn/s/blog_ad7fd0f4010180md.html

(一)

儲存

var xml =XElement.Load(@"路徑");
xml.Element("節點名字").AddAfterSelf(new XElement("節點名字","要新增的值"));
xml,Save(@"路徑");
讀取
var xml =XElement.Load(@"路徑");
如果是屬性
var query=xml.Element().Where(n=>n.Attribute("比較的節點名字").Value=="名字")
.Select(n=>n.Attribute("要獲取的節點名字").Value).Frist();

如果是值
var query=xml.Element().Where(n=>n.Value=="名字")

.Select(n=>n.Value).Frist();

(二)

直接用專案裡面的app.config或是web.config最方便。
在裡面的appSettings段里加一個元素:
<appSettings>
<add key="mypath" value="thepath"/>
</appSettings>

可以直接用ConfigurationManager讀取:
string pathStr = ConfigurationManager.AppSettings["mypath"].ToString();;


(三)

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格式正確

會提示

缺少根元素

更改一致就可以了