[C#] Hướng Dẫn Thêm Xóa Sửa XML File Trong Winform Linq
Có thể bạn quan tâm
Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách làm việc với tập tin file XML sử dụng XML LinQ trong C#, winform.
[C#] CRUD XML File using LinQ Winform![]()
Vậy file XML là gì?
XML là từ viết tắt của từ Extensible Markup Language là ngôn ngữ đánh dấu mở rộng.
XML có chức năng truyền dữ liệu và mô tả nhiều loại dữ liệu khác nhau. Tác dụng chính của XML là đơn giản hóa việc chia sẻ dữ liệu giữa các nền tảng và các hệ thống được kết nối thông qua mạng Internet.
Chúng ta thường sử dụng tập tin XML để lưu trữ các thông tin cấu hình của phần mềm ứng dụng.
Bài này mình sẽ thao tác với tập tin XML Employee Data như dưới đây:
<?xml version="1.0" encoding="utf-8"?> <Employees> <Employee> <id>2</id> <fullname>Trịnh Quốc Khang</fullname> <salary>67800000</salary> <email>[email protected]</email> <address>Biên Hòa - Đồng Nai</address> </Employee> <Employee> <id>3</id> <fullname>Nguyễn Thảo</fullname> <salary>67800000</salary> <email>[email protected]</email> <address>Biên Hòa - Đồng Nai</address> </Employee> <Employee> <id>4</id> <fullname>Nguyễn Đình Tuyên</fullname> <salary>67800000</salary> <email>[email protected]</email> <address>Biên Hòa - Đồng Nai</address> </Employee> </Employees>Video demo ứng dụng CRUD XML C#:
Full source code C#:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; namespace CRUDXmlFile { public partial class Form1 : Form { private XDocument xmldoc; private string URL_XML_FILE = "data_employees.xml"; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { LoadDataEmployees(); } public void LoadDataEmployees() { xmldoc = XDocument.Load(URL_XML_FILE); var data = xmldoc.Descendants("Employee").Select(p => new { Id = p.Element("id").Value, FullName = p.Element("fullname").Value, Salary = p.Element("salary").Value, Email = p.Element("email").Value, Address = p.Element("address").Value }).OrderBy(p => p.Id).ToList(); txt_id.DataBindings.Clear(); txt_fullname.DataBindings.Clear(); txt_salary.DataBindings.Clear(); txt_email.DataBindings.Clear(); txt_address.DataBindings.Clear(); txt_id.DataBindings.Add("text", data, "id"); txt_fullname.DataBindings.Add("text", data, "fullname"); txt_salary.DataBindings.Add("text", data, "salary"); txt_email.DataBindings.Add("text", data, "email"); txt_address.DataBindings.Add("text", data, "address"); dataGridView1.DataSource = data; } private void btn_add_Click(object sender, EventArgs e) { foreach (var item in group_info.Controls) { if(item is TextBox) { (item as TextBox).Clear(); } } txt_id.Focus(); } private void btn_save_Click(object sender, EventArgs e) { XElement emp = new XElement("Employee", new XElement("id", txt_id.Text), new XElement("fullname", txt_fullname.Text), new XElement("salary", txt_salary.Text), new XElement("email", txt_email.Text), new XElement("address", txt_address.Text)); xmldoc.Root.Add(emp); xmldoc.Save(URL_XML_FILE); LoadDataEmployees(); btn_add_Click(null, null); } private void btn_update_Click(object sender, EventArgs e) { XElement emp = xmldoc.Descendants("Employee").FirstOrDefault(p => p.Element("id").Value == txt_id.Text); if (emp != null) { emp.Element("fullname").Value = txt_fullname.Text; emp.Element("salary").Value = txt_salary.Text; emp.Element("email").Value = txt_email.Text; emp.Element("address").Value = txt_address.Text; xmldoc.Save(URL_XML_FILE); LoadDataEmployees(); btn_add_Click(null, null); } } private void btn_delete_Click(object sender, EventArgs e) { XElement emp = xmldoc.Descendants("Employee").FirstOrDefault(p => p.Element("id").Value == txt_id.Text); if (emp != null) { emp.Remove(); xmldoc.Save(URL_XML_FILE); LoadDataEmployees(); btn_add_Click(null, null); } } } }Thanks for watching!
DOWNLOAD SOURCE
Tags: crud xml c#read xml file c#write file xml c#THÔNG TIN TÁC GIẢ
Founder 1289 bài viết 17,155,372 NGUYỄN THẢO
Founder at LaptrinhVB.net
★★★★★
♥ Tình yêu thương chẳng hề hư mất bao giờ. (Cr 13,4)
=========================================================================
My skills includes .NET(C#, VB.NET), DevExpress, Java, Android, PHP,
Python, Sqlserver, Mysql, Reactjs, Dart, Flutter, API services and lot more...
Phone/Zalo/Telegram/WhatsApp: ☎️ (+84)933.913122
Zalo: https://zalo.me/0933913122
Email: [email protected]
My Github: https://github.com/nguyenthao1988
Facebook: https://fb.com/Lewandowski28031988
Youtube Channel: https://www.youtube.com/@thaomeotv
=========================================================================
" Gửi tặng tác giả: Thảo Meo ly cà phê đầy năng lượng và cảm hứng. "

BÀI VIẾT LIÊN QUAN
Từ khóa » Xóa Xml
-
Xóa Thông Tin ánh Xạ XML Khỏi Sổ Làm Việc - Microsoft Support
-
Delete XML Map Information From A Workbook - Microsoft Support
-
Deleting XML Documents - IBM
-
Delete XML Parser Select Entry - Oracle Help Center
-
How Odoo Proceed To Delete Absent Xml Record?
-
Hide Delete In Odoo 14 - Xml - Stack Overflow
-
Delete Configuration - Palo Alto Networks
-
Java Delete Files - W3Schools
-
Method: Nokogiri::XML::NodeSet#delete
-
Delete Resources - Oxygen XML Editor
-
How To Delete XML Messages From Sxmb_moni - SAP Answers
-
Delete Xml Tags That Don't Contain Any Data - Super User
-
Step 4: Zip XML File(s) Or Upload / Delete Zip File
-
Delete XML File Command Option - Advanced Installer