C # Read xml files to the dview instance

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.