天天看點

C# 對XML操作-執行個體

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.Xml.Linq;   // 添加

namespace test

{

    public partial class frm_TestMain : Form

    {

        private XDocument xdocCFG= new XDocument();//建立一個document檔案

        string str=null;

        public frm_TestMain()

        {

            InitializeComponent();

            xdocCFG = XDocument.Load("testCFG.xml");//加載配置檔案XML

            var result= from c in xdocCFG.Descendants("Jife") select c;//選中以Jife為父節點的子節點

            foreach(var item in result)

            {

                string strValue = item.Element("age").Value;  //選取age标志下的在資料

                str += strValue+"#";    //消息累加,便于顯示。

            }

        }

        private void Form1_Load(object sender, EventArgs e)

        {

        }

        private void btn_show_Click(object sender, EventArgs e)

        {

            MessageBox.Show(str); //彈出

        }

    }

}

産生效果如圖:

C# 對XML操作-執行個體

腳本檔案,dat

C# 對XML操作-執行個體