手机
当前位置:查字典教程网 >编程开发 >C#教程 >c#读取xml文件到datagridview实例
c#读取xml文件到datagridview实例
摘要:复制代码代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Compo...

复制代码 代码如下:

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节点的详细信息

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)//遍历查询结果

{

textBox11.Text = element.Element("Name").Value;//显示职工姓名

comboBox1.SelectedItem = element.Element("Sex").Value;//显示职工性别

textBox12.Text = element.Element("Salary").Value;//显示职工薪水

}

}

#region 将XML文件内容绑定到DataGridView控件

/// <summary>

/// 将XML文件内容绑定到DataGridView控件

/// </summary>

private void getXmlInfo()

{

DataSet myds = new DataSet();

myds.ReadXml(strPath);

dataGridView1.DataSource = myds.Tables[0];

}

#endregion

}

}

复制代码 代码如下:

<?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>

【c#读取xml文件到datagridview实例】相关文章:

c# 获得局域网主机列表实例

C#中使用反射获取结构体实例及思路

c# 控件截图的简单实例

C#简单获取时间差的小例子

C#读写文件的方法汇总

C#生成不重复随机数列表实例

C#委托初级使用的实例代码

C#操作EXCEL DataTable转换的实例代码

c# 可疑文件扫描代码(找到木马)(简)

c# 共享状态的文件读写实现代码

精品推荐
分类导航