c#读取xml文件到datagridview实例
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 文件 报错:未将对象引用设置到对象的实例
- HOW TO:使用 Visual C# .NET 从文件中读取 XML
- [C#基础]用反射、泛型读取xml后动态创建实例并赋值
- c#读取xml文件到datagridview中
- 合理使用DataSet和DataGridView读取xml文件到DataGridView
- 黑马程序员之C#学习笔记: xml文件读取
- 教授写的读取xml文件信息实例
- C# 读取xml中配置文件中的信息
- C#控制台基础 在博客备份xml文件中提取所有博文的标题 (正则,流读取)
- 最好是给一个实例 c#怎么样 读取word文件
- C#创建及读取xml文件
- 详解c#读取XML的实例代码
- C#读取剪贴板内容并把内容记录在XML文件中
- C#读取含有xmlns的XML文件
- C#读取XML文件简单介绍
- 在C#中创建和读取XML文件
- C#将xml文件数据导入datagridview中
- C#读取写入带有命名空间的XML文件
- java配置文件之xml和properties简单读取实例
- C#2005如何读取XML文件中的数据库连接字符串??? (好像和2003不一样啊) 急,跪求!