使用LINQ技术对XML文件进行读取
发布时间:2020-12-16 05:18:05 所属栏目:百科 来源:网络整理
导读: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;namespac
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;//显示职工薪水 } } /// 将XML文件内容绑定到DataGridView控件 /// <summary> /// 将XML文件内容绑定到DataGridView控件 /// </summary> private void getXmlInfo() { DataSet myds = new DataSet(); myds.ReadXml(strPath); dataGridView1.DataSource = myds.Tables[0]; } } }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |