国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#讀取xml文件到datagridview實例

2020-01-24 03:31:13
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

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.IO;
using System.Xml;
using System.Xml.Linq;

namespace QueryXMLByLINQ
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        static string strPath = "Employee.xml";
        static string strID = "";

        //窗體加載時加載XML文件
        private void Form1_Load(object sender, EventArgs e)
        {
            getXmlInfo();
        }

        //顯示選中XML節(jié)點的詳細信息
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            strID = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();//記錄選擇的職工編號
            XElement xe = XElement.Load(strPath);//加載XML文件
            //使用LINT從XML文件中查詢信息
            IEnumerable<XElement> elements = from PInfo in xe.Elements("People")
                                             where PInfo.Attribute("ID").Value == strID
                                             select PInfo;
            foreach (XElement element in elements)//遍歷查詢結(jié)果
            {
                textBox11.Text = element.Element("Name").Value;//顯示職工姓名
                comboBox1.SelectedItem = element.Element("Sex").Value;//顯示職工性別
                textBox12.Text = element.Element("Salary").Value;//顯示職工薪水
            }
        }

        #region 將XML文件內(nèi)容綁定到DataGridView控件
        /// <summary>
        /// 將XML文件內(nèi)容綁定到DataGridView控件
        /// </summary>
        private void getXmlInfo()
        {
            DataSet myds = new DataSet();
            myds.ReadXml(strPath);
            dataGridView1.DataSource = myds.Tables[0];
        }
        #endregion
    }
}

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
-<Peoples> -<People ID="001"> <Name>小王</Name> <Sex>男</Sex> <Salary>1500</Salary> </People> -<People ID="002"> <Name>小呂</Name> <Sex>男</Sex> <Salary>1500</Salary> </People> -<People ID="003"> <Name>小梁</Name> <Sex>男</Sex> <Salary>1500</Salary> </People> </Peoples>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 石狮市| 自治县| 重庆市| 延川县| 九江县| 神木县| 湘潭市| 汉阴县| 祁连县| 灌阳县| 运城市| 施甸县| 桐乡市| 建阳市| 天津市| 大埔县| 广南县| 巴林左旗| 盱眙县| 张北县| 宜宾市| 久治县| 元阳县| 集贤县| 息烽县| 安达市| 梁山县| 玉树县| 江油市| 威海市| 平顺县| 元氏县| 民丰县| 奇台县| 娄烦县| 屏东县| 张家港市| 宣汉县| 桐梓县| 临夏县| 黎平县|