Copy codeThe Code is as follows: 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 = "";
// Load the XML file when loading the form
Private void Form1_Load (object sender, EventArgs e)
{
GetXmlInfo ();
}
// Display the details of the selected XML Node
Private void maid cellclick (object sender, maid e)
{
StrID = maid [e. RowIndex]. Cells [3]. Value. ToString (); // record the selected employee ID
XElement xe = XElement. Load (strPath); // Load the XML file
// Use LINT to query information from the XML file
IEnumerable <XElement> elements = from PInfo in xe. Elements ("People ")
Where PInfo. Attribute ("ID"). Value = strID
Select PInfo;
Foreach (XElement element in elements) // traverses the query results
{
TextBox11.Text = element. Element ("Name"). Value; // display employee Name
ComboBox1.SelectedItem = element. Element ("Sex"). Value; // display employee gender
TextBox12.Text = element. Element ("Salary"). Value; // display employee salaries
}
}
# Region binds the XML file content to the DataGridView Control
/// <Summary>
/// Bind the XML file content to the DataGridView Control
/// </Summary>
Private void getXmlInfo ()
{
DataSet myds = new DataSet ();
Myds. ReadXml (strPath );
DataGridView1.DataSource = myds. Tables [0];
}
# Endregion
}
}
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
-<Les>-<People ID = "001"> <Name> JOHN </Name> <Sex> male </Sex> <Salary> 1500 </Salary> </People >-<People ID = "002"> <Name> Xiao Lu </Name> <Sex> male </Sex> <Salary> 1500 </Salary> </People>-< people ID = "003"> <Name> xiaoliang </Name> <Sex> male </Sex> <Salary> 1500 </Salary> </People> </LES>