-
03-02-2008, 09:16 AM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Xử lý XML trong lập trình C# .NET
KỲ 1
MỞ MỘT TÀI LIỆU XML
Để mở một tài liệu XML, bạn cần sử dụng lớp XmlDocument. Lớp XmlDocument cho phép bạn mở tài liệu XML theo 3 cách thông dụng sau:
-Bạn có thể mở thông qua 1 đường dẫn trên ổ cứng, một URL, hoặc 1 tập tin XML.
-Bạn có thể sử dụng đối tượng stream như FileStream chứa dữ liệu của tài liệu XML.
-Bạn có mở thông qua 1 chuỗi chứa XML trong bộ nhớ
Để thấy được các đặc tính này, trong kỳ 1 này, chúng ta cùng làm thử 1 ứng dụng nhỏ như con thỏ nhé:
Ứng dụng của chúng ta bao gồm: 3 radio button cho việc chọn nơi chứa tài liệu mà lớp XmlDocument sẽ mở. 1 textbox dùng để nhập đường dẫn đến tập tin xml, hoặc URL hoặc 1 string chứa chuỗi XML. Cuối cùng là 1 button dùng để thực thi các câu lệnh để mở tài liệu. Sau đây là đoạn code thụ lý tình huống Click của button:
Mã:private void Button1_Click(object sender, System.EventArgs e) { try { string path = TextBox1.Text; XmlDocument document = new XmlDocument(); if (RadioButton1.Checked == true) { document.Load(path); } if (RadioButton2.Checked == true) { System.IO.FileStream fs; fs = System.IO.File.OpenRead(path); document.Load(fs); } if (RadioButton3.Checked == true) { document.Load(path); } MessageBox.Show("Mở tài liệu XML thành công!"); } catch (Exception ex) { MessageBox.Show(ex.Message); }}
Ở đây, có điều cần lưu ý với các bạn là: nhớ imports namespace: System.Xml nhé, vì đây chính là namespace chứa class XmlDocument, và là 1 trong các namespace chủ lực của .NET hỗ trợ làm việc với XML.
Để tiện cho mọi người thực hành, mình up lên đây: 1 file xml để thực hành (employees.xml), và mã nguồn của chương trình đầu tiên này.
Tác giả: neverlandView more random threads:
- tạo webservice đơn giản giải phương trình bậc hai
- Lập trình Winsock trong C# | Winsock programing with C#
- write, read, append file | xuất, đọc, sửa file
- Tìm hiểu cơ chế và sử dụng từ khóa “yield” trong C#
- Tạo Database bằng Lập trình C#.
- Cách dùng delegate trong lập trình C#
- Class Xử lý số lớn (+ , - , *)
- Đọc từng byte của file trong lập trình C#?
- 10 mẹo để trau dồi kỹ năng lập trình
- Hiển thị các files trong Folder bằng C#
-
03-02-2008, 09:22 AM #2Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
KỲ 2
DI CHUYỂN XUYÊN QUA TÀI LIỆU XML
Một tài liệu XML bao gồm 1 hoặc nhiều nút, và có những nút nằm bên trong nút khác. Những nút nằm bên trong nút khác gọi là nút con.
Lớp XmlNode có 1 tập hợp gọi là ChildNodes mà chứa danh sách tất cả các nút con của một nút bất kỳ. Ngoài ra, chúng còn có các thuộc tính: ParentNode, FirstChild, LastChild, NextSibling và PreviousSibling mà cho phép bạn di chuyển đến bất kỳ nút nào. Như thuộc tính ParentNode giúp bạn di chuyển đến nút cha của 1 nút nào đó, hoặc thuộc tính NextSibling giúp bạn di chuyển đến nút kế tiếp ở cùng cấp của nút hiện hành.
Kỳ này, chúng ta cũng thử làm 1 project nhỏ:
Ứng dụng của chúng ta chỉ có 1 treeview. Mục đích của chúng ta là hiển thị cấu trúc của tập tin XML lên treeview lúc chương trình được Load lên.
Mã:private void Form1_Load(object sender, System.EventArgs e) { string path = Application.StartupPath + "\\employees.xml"; XmlDocument document = new XmlDocument(); document.Load(path); TreeNode rootnode = TreeView1.Nodes.Add(document.DocumentElement.Name); XmlNodeList nodes = document.DocumentElement.ChildNodes; foreach (XmlNode childnode in nodes) { TreeNode empNode = new TreeNode(childnode.Name + ":" + childnode.Attributes("employeeid").Name + "=" + childnode.Attributes("employeeid").Value); foreach (XmlNode node in childnode.ChildNodes) { if (node.Name == "firstname") { empNode.Nodes.Add("First Name:" + node.InnerText); } if (node.Name == "lastname") { empNode.Nodes.Add("Last Name:" + node.InnerText); } if (node.Name == "homephone") { empNode.Nodes.Add("Home Phone:" + node.InnerText); } if (node.Name == "notes") { empNode.Nodes.Add("Notes:" + node.InnerText); } } rootnode.Nodes.Add(empNode); } }
Đoạn code tạo 1 thể hiện của lớp XmlDocument và nạp tập tin employees.xml được đặt trong cùng thư mục của file thực thi của chương trình. Nút gốc của tài liệu xml có thể được truy cập thông qua thuộc tính DocumentElement của đối tượng thể hiện của lớp XmlDocument. Thuộc tính Name của DocumentElement trả về tên của nút gốc này (employees).
Nút employees chứa 3 nút employee, để truy cập 3 nút này, ta dùng thuộc tính ChildNodes của DocumentElement mà ta đã nói ở trên. Tà dùng vòng lặp foreach để đi lần lượt qua từng nút employee một.
Để truy cập attribute employeeid, ta sử dụng collection Attribute của lớp XmlNode. Bạn có thể xác định attribute nào cần lấy thông qua chỉ mục hoặc tên của nó.
Để nhận được chuỗi nằm giữa 1 cặp tag, ta dùng thuộc tính InnerText của XmlNode.
Tác giả: neverland
-
03-02-2008, 09:31 AM #3Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
KỲ 3
NHẬN CÁC PHẦN TỬ BẤT KỲ BẰNG CÁCH SỬ DỤNG PHƯƠNG THỨC GetElementByTagName()
Lớp XmlDocument có phương thức GetElementByTagName() giúp bạn tìm ra những node có tag do bạn chỉ định. Chẳng hạn, trong employees.xml có 3 nút employee. Mình nhập chuỗi "employee" thì nó sẽ trả về cho mình 3 nút này dễ dàng. Cùng làm thử 1 ứng dụng với mình nhé!
Ứng dụng của chúng ta gồm 1 textbox để nhập tên tag cần lấy, 1 listbox để hiện thi danh sách các nút có tag tìm được, 1 textbox để hiển thị nội dung của nút đó. Và 1 button để thực thi câu lệnh dùng để lấy nút thông qua tên tag.
Trước khi thụ lý, tình huống Click của button, bạn cần khai báo 1 biến thành viên của class như sau:
Mã nguồn PHP:XmlNodeList nodelist;
Bây giờ mới đến lúc thụ lý tình huống Click của button nè:
Mã:private void Button1_Click(object sender, System.EventArgs e) { //làm sạch listbox ListBox1.Items.Clear(); //mở tài liệu XML XmlDocument doc = new XmlDocument(); doc.Load(Application.StartupPath + "\\employees.xml"); //đưa tất cả các nút tìm được qua tên tag //vào nodelist nodelist = doc.GetElementsByTagName(TextBox1.Text); //đi lần lượt qua từng node cuaa nodelist //và đưa nó vào listbox foreach (XmlNode node in nodelist) { ListBox1.Items.Add(node.Name); } }
Mã:private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e) { TextBox2.Text = nodelist(ListBox1.SelectedIndex).InnerText; }
-
03-02-2008, 09:37 AM #4Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
KỲ 4
CHỌN NHIỀU NÚT BẤT KỲ BẰNG CÁCH SỬ DỤNG PHƯƠNG THỨC SelectNodes()
Lớp XmlDocument có phương thức SelectNodes() mà chấp nhận 1 chuỗi chứa câu truy vấn để tìm lọc ra những nút mà bạn muốn, nó sẽ trả về đối tượng XmlNodeList chứa danh sách tất cả các nút thỏa mãn điều kiện truy tìm.
Để hiểu rõ phương thức này, chúng ta cùng làm thử 1 ứng dụng nhỏ:
Ứng dụng của chúng ta bao gồm:
-1 textbox để điền tên của 1 người nào đó
-2 radio button: 1 để chọn tìm kiếm theo tên, 1 để chọn tìm kiếm theo họ.
-2 button
-10 textbox
-1 combobox để hiện thị mã nhân viên (employeeid) tương ứng với ông nhân viên mà bạn tìm.
Trước khi thụ lý các tình hướng, bạn làm:
-Imports System.Xml
-Khai báo 1 biến thành viên:
Dim nodelist As XmlNodeList
-Thụ lý tình huống Click của nút “Tìm”:
Mã:private void Button1_Click(object sender, System.EventArgs e) { //mở tập tin employees.xml XmlDocument doc = new XmlDocument(); doc.Load(Application.StartupPath + "\\employees.xml"); /ếu chọn radio button "Tên" if (RadioButton1.Checked == true) { //chọn phần tử employee nào //mà có ông con firstname nào = <textbox1.Text> //trả về các nút employee hợp lệ vào biến nodelist nodelist = doc.SelectNodes("/employees/employee[./firstname/text() = '" + TextBox1.Text + "']"); } /ếu chọn radio button "Họ" if (RadioButton2.Checked) { //chọn phần tử employee nào //mà có ông con lastname nào = <textbox1.Text> //trả về các nút employee hợp lệ vào biến nodelist nodelist = doc.SelectNodes("/employees/employee[./lastname/text() = '" + TextBox1.Text + "']"); } //đi xuyên qua các nút employee //của tập hợp các nút employee vừa tìm được ở trên foreach (XmlNode node in nodelist) { ComboBox1.Items.Add(node.Attributes("employeeid").Value); } ComboBox1.SelectedIndex = 0; }
/employees/employee[./firstname/text() = '" + TextBox1.Text + "']"
Nhu vậy, ta dịch câu truy vấn này như sau: “hãy cho tôi các nút employee mà có firstname = 1 tên chỉ định trong textbox1.
Hàm text() là hàm trả về đoạn text của 1 nút (ở đây là nút firstname).
Bạn lưu ý là nó sẽ trả về các nút employee, chứ không phải các nút firstname hay lastname gì đâu nhé.
Lúc này, nếu tìm được các nút thỏa điều kiện, user sẽ chọn mã số nhân viên trong combobox, và nhấn nút “Hiển thị” để cho biết các thông tin của nhân viên ứng với mã số nhân viên đó. Vậy, ta có đoạn code thụ lý tình huống Click của nút “Hiển thị” như sau:
Mã:private void Button2_Click(object sender, System.EventArgs e) { //label FirstName = giá trị nằm giữa cặp tag <firstname>...</firstname> Label8.Text = nodelist(ComboBox1.SelectedIndex).ChildNodes(0).InnerText; //label LastName = giá trị nằm giữa cặp tag <lastname>...</lastname> Label9.Text = nodelist(ComboBox1.SelectedIndex).ChildNodes(1).InnerText; //label HomePhone = giá trị nằm giữa cặp tag <homephone>...</homephone> Label10.Text = nodelist(ComboBox1.SelectedIndex).ChildNodes(2).InnerText; //label notes = giá trị nằm giữa cặp tag <notes>...<otes> Label11.Text = nodelist(ComboBox1.SelectedIndex).ChildNodes(3).InnerText; }
-
04-02-2008, 08:31 AM #5Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
KỲ 5
CHỌN MỘT NÚT BẤT KỲ BẰNG CÁCH SỬ DỤNG PHƯƠNG THỨC SelectSingleNode()
Ở kỳ 4, bạn đã được biết qua chức năng của phương thức SelectNodes() của class XmlDocument, nhưng cũng có nhiều lúc bạn chỉ muốn chọn một nút mà thôi. Tất nhiên là vẫn có thể dùng SelectNodes(), tuy nhiên có vẻ không hợp lý lắm. Bởi thế, class XmlNode còn cung cấp cho bạn phương thức SelectSingleNode() cho việc chọn 1 nút.
Để dễ dàng theo dõi, mình post lại nội dung của tập tin employees.xml:
Mã:<?xml version="1.0" encoding="utf-8" ?> <!-- This is list of employees --> <employees> <employee employeeid="1"> <firstname>Nancy</firstname> <lastname>Davolio</lastname> <homephone>(206) 555-9857</homephone> <notes> <![CDATA[includes a BA in psychology from Colorado State University in She also completed "The Art of the Cold Call." Nancy is a member of Toastmasters International.]]> <otes> </employee> <employee employeeid="2"> <firstname>Andrew</firstname> <lastname>Fuller</lastname> <homephone>(206) 555-9482</homephone> <notes> <![CDATA[Andrew received his BTS commercial in 1974 and a Ph.D. in international marketing from the University of Dallas in He is fluent in French and Italian and reads German. He joined the company as a sales representative, was promoted to sales manager in January 1992 and to vice president of sales in March Andrew is a member of the Sales Management Roundtable, the Seattle Chamber of Commerce, and the Pacific Rim Importers Association.]]> <otes> </employee> <employee employeeid="3"> <firstname>Janet</firstname> <lastname>Leverling</lastname> <homephone>(206) 555-3412</homephone> <notes> <![CDATA[Janet has a BS degree in chemistry from Boston College (1984). She has also completed a certificate program in food retailing management. Janet was hired as a sales associate in 1991 and promoted to sales representative in February 1992.]]> <otes> </employee> </employees>
- Thay thế biến thành viên:
Mã nguồn PHP:XmlNodeList nodeList;
Mã nguồn PHP:XmlNode nodeSelect;
Mã:private void Button1_Click(object sender, System.EventArgs e) { XmlDocument doc = new XmlDocument(); doc.Load(Application.StartupPath + "\\employees.xml"); if (RadioButton1.Checked == true) { nodeSelect = doc.SelectSingleNode("/employees/employee[./firstname/text() = '" + TextBox1.Text + "']"); } if (RadioButton2.Checked) { nodeSelect = doc.SelectSingleNode("/employees/employee[./lastname/text() = '" + TextBox1.Text + "']"); } if (nodeSelect != null) { ComboBox1.Items.Add(nodeSelect.Attributes("employeeid").Value); } ComboBox1.SelectedIndex = 0; }
Mã:private void Button2_Click(object sender, System.EventArgs e) { Label8.Text = nodeSelect.ChildNodes(0).InnerText; Label9.Text = nodeSelect.ChildNodes(1).InnerText; Label10.Text = nodeSelect.ChildNodes(2).InnerText; Label11.Text = nodeSelect.ChildNodes(3).InnerText; }
Tác giả: neverland
-
04-02-2008, 08:39 AM #6Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
KỲ 6
THÊM, XÓA, CẬP NHẬT NÚT TRONG TÀI LIỆU XML
Cho đến thời điểm này, có lẽ các bạn cũng đã khá rành cách chọn 1 nút trên tài liệu, nhưng đã chọn được rồi, thì tất nhiên là sẽ có những lúc bạn muốn thêm, xóa hoặc chỉnh sửa 1 nút nào đó. Trong kỳ này, mình sẽ hướng dẫn các bạn xử lý những vấn đề này.
Chúng ta sẽ làm thử 1 ứng dụng như hình dưới:
Ứng dụng của chúng ta gồm:
-6 label, trong đó label6 là label dùng để hiện thị vị trí của nút hiện hành (lblRecord).
-1 combobox để hiện mã số nhân viên
-4 textbox dùng để hiện thị thông tin nhân viên
-3 button tương ứng với 3 tính năng: thêm,cập nhật, xóa.
-4 button để di chuyển trên tài liệu: về đầu, lùi 1 nút, tăng 1 nút, về cuối.
Đầu tiên, bạn nhớ Imports System.Xml rồi sau đó khai báo 2 biến thành viên:
Mã nguồn PHP:int chimuc = 0; XmlDocument doc = new XmlDocument();
Mã:private void Form1_Load(object sender, System.EventArgs e) { doc.Load(Application.StartupPath + "\\employees.xml"); foreach (XmlNode node in doc.DocumentElement.ChildNodes) { cboEmployeeID.Items.Add(node.Attributes("employeeid").Value); } cboEmployeeID.SelectedIndex = 0; FillControls(); UpdateLabel(); }
Kế đến là 2 phương thức FillControls (điền đầy thông tin nhân viên vào các textbox thông tin) và UpdateLabel (cập nhật vị trí nút trên lblRecord). 2 phương thức này, tí nữa chúng ta sẽ xây dựng.
Bây giờ, chúng ta tiếp tục thụ lý tình huống Click cho 4 button di chuyển trên tài liệu:
Mã:private void btnFirst_Click(object sender, System.EventArgs e) { chimuc = 0; FillControls(); UpdateLabel(); } private void btnPrevious_Click(object sender, System.EventArgs e) { chimuc = chimuc - 1; if (chimuc < 0) { chimuc = 0; } FillControls(); UpdateLabel(); } private void btnNext_Click(object sender, System.EventArgs e) { chimuc = chimuc + 1; if (chimuc >= doc.DocumentElement.ChildNodes.Count - 1) { chimuc = doc.DocumentElement.ChildNodes.Count - 1; } FillControls(); UpdateLabel(); } private void btnLast_Click(object sender, System.EventArgs e) { chimuc = doc.DocumentElement.ChildNodes.Count - 1; FillControls(); UpdateLabel(); }
Giờ đây, chúng ta bắt đầu xây dựng 2 phương thức FillControl và UpdateLabel để hoàn tất nửa chặng đường đầu tiên:
Mã://Fill Controls public void FillControls() { /ode = nút con th? <chimuc> c?a nút g?c c?a tài li?u XmlNode node = doc.DocumentElement.ChildNodes(chimuc); //combobox mã s? nhân viên = giá tr? c?a attribute employeeid c?a nút con v?a tìm cboEmployeeID.Text = node.Attributes("employeeid").Value; //gán chu?i n?m gi?a c?p tag ?óng-m? cho các textbox thông tin txtFirstName.Text = node.ChildNodes(0).InnerText; txtLastName.Text = node.ChildNodes(1).InnerText; txtHomePhone.Text = node.ChildNodes(2).InnerText; txtNotes.Text = node.ChildNodes(3).InnerText; } //Update Label public void UpdateLabel() { //doc.Document.ChildNodes.Count = s? nút con c?a nút g?c c?a tài li?u lblRecord.Text = (chimuc + 1).ToString + "/" + doc.DocumentElement.ChildNodes.Count; }
Dễ nhất là cập nhật (UPDATE) thông tin cho một nút:
-Đoạn mã thụ lý tình huống Click của button UPDATE
Mã:private void btnUpdate_Click(object sender, System.EventArgs e) { //ch?n 1 nút t??ng ?ng v?i mã nhân viên ???c ch?n t? cboEmployeeID XmlNode node = doc.SelectSingleNode("employees/employee[@employeeid = '" + cboEmployeeID.SelectedItem + "']"); /?u có nhân viên t??ng ?ng if (node != null) { //gán các giá tr? trong các textbox thông tin //vào gi?a c?p tag ?óng-m? c?a các nút con c?a nút ?ang xét //? ?ây là các tag: firstname,lastname,notes c?a nút employee node.ChildNodes(0).InnerText = txtFirstName.Text; node.ChildNodes(1).InnerText = txtLastName.Text; node.ChildNodes(2).InnerText = txtNotes.Text; //riêng v?i CData, ta ph?i làm b?ng cách t?o 1 bi?n ki?u XmlCDataSection //ph??ng th?c doc.CreateCDataSection s? giúp ta t?o 1 CData d?a trên 1 chu?i ???c truy?n vào ph??ng th?c XmlCDataSection notes = doc.CreateCDataSection(txtNotes.Text); //thay th? CDataSection c? là : node.ChildNodes(3).ChildNodes(0) //b?i nút notes node.ChildNodes(3).ReplaceChild(notes, node.ChildNodes(3).ChildNodes(0)); //l?u l?i tài li?u doc.Save(Application.StartupPath + "\\employees.xml"); } }
Đoạn mã thụ lý tình huống Click của button DELETE:
Mã:private void btnDelete_Click(object sender, System.EventArgs e) { //ch?n 1 nút t??ng ?ng v?i mã nhân viên ???c ch?n t? cboEmployeeID XmlNode node = doc.SelectSingleNode("employees/employee[@employeeid = '" + cboEmployeeID.SelectedItem + "']"); /?u có nhân viên t??ng ?ng if (node != null) { //di chuy?n nút employee t??ng ?ng kh?i tài li?u //thông qua ph??ng th?c RemoveChild(<nut can xoa>) //c?a doc.DocumentElement doc.DocumentElement.RemoveChild(node); } //l?u l?i tài li?u doc.Save(Application.StartupPath + "\\employees.xml"); //C?p nh?t l?i s? v? trí c?a nút UpdateLabel(); }
Đoạn mã thụ lý tình huống Click của Button ADD
Mã:private void btnAdd_Click(object sender, System.EventArgs e) { //khai báo các bi?n ph?n t? (XmlElement) c?a tài li?u //?ng v?i tên c?a chúng ???c truy?n vào ph??ng th?c CreateElement XmlElement employee = doc.CreateElement("employee"); XmlElement firstname = doc.CreateElement("firstname"); XmlElement lastname = doc.CreateElement("lastname"); XmlElement homephone = doc.CreateElement("homephone"); XmlElement notes = doc.CreateElement("notes"); //khai báo 1 bi?n t??ng tr?ng cho attribute employeeid (XmlAttribute) //v?i tên là chu?i ???c truy?n vào ph??ng th?c CreateAttribute XmlAttribute employeeid = doc.CreateAttribute("employeeid"); //gán giá tr? c?a cboEmployeeID vào //bi?n attribute employeeid v?a t?o employeeid.Value = cboEmployeeID.Text; //khai báo các bi?n t??ng tr?ng cho chu?i n?m gi?a c?p tag ?óng-m? c?a 1 nút b?t k? //v?i giá tr? là chu?i ???c truy?n vào ph??ng th?c CreateTextNode XmlText firstnametext = doc.CreateTextNode(txtFirstName.Text); XmlText lastnametext = doc.CreateTextNode(txtLastName.Text); XmlText homephonetext = doc.CreateTextNode(txtHomePhone.Text); //riêng ??i v?i ?o?n v?n b?n n?m trong tag CData, ta ph?i làm cách khác v?i cách XmlText //t?o 1 bi?n ki?u XmlCDataSection XmlCDataSection notestext = doc.CreateCDataSection(txtNotes.Text); //ph?n t? notes thêm notestext vào b?n thân nó. notes.AppendChild(notestext); //ph?n t? employee thêm attribute employeeid vào b?n thân nó employee.Attributes.Append(employeeid); //cho ph?n t? employee có các nút con là : firstname,lastname,homephone và notes employee.AppendChild(firstname); employee.AppendChild(lastname); employee.AppendChild(homephone); employee.AppendChild(notes); //thêm ph?n t? employee v?a t?o vào nút g?c employees c?a tài li?u doc.DocumentElement.AppendChild(employee); //l?u l?i tài li?u doc.Save(Application.StartupPath + "\\employees.xml"); //c?p nh?t l?i thông tin v? trí c?a nút UpdateLabel(); }
-
04-02-2008, 08:42 AM #7Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
KỲ 7
LIÊN HỆ VỚI KHOẢNG TRẮNG
Từ đầu đến giờ, bạn đã được học về cách đọc và viết tài liệu XML sử dụng lớp XmlDocument. Trong quá trình đọc viết đó, chúng ta khó tránh khỏi sự bực mình với khoảng trắng (là ký tự khoảng trắng,tab,…) vì mặc định khi tài liệu XML được đọc (dùng phương thức Load hoặc LoadXml) hoặc lưu lại (phương thức Save), nó sẽ loại bỏ khoảng trắng (ignore white space). Kỳ này, mình sẽ chỉ cho các bạn cách đối xử đối với lũ khoảng trắng này.
Để dễ thấy sự khác biệt giữa việc loại bỏ hay không loại bỏ khoảng trắng, chúng ta thử làm 1 ví dụ đơn giản như sau:
Đây là đoạn mã thụ lý tình huống Click của button:
Mã:private void Button1_Click(object sender, System.EventArgs e) { XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = CheckBox1.Checked; doc.Load(Application.StartupPath + "\\employees.xml"); MessageBox.Show(doc.InnerXml); }
-Khi checkbox không được chọn:
-Khi checkbox được chọn:
Okay, bây giở bạn thử thay thế dòng code:
Mã:MessageBox.Show(doc.InnerXml);
Mã:MessageBox.Show("Nút employees có: " + doc.DocumentElement.ChildNodes.Count.ToString() + " nút con");
Tác giả: neverland
-
24-03-2009, 06:50 PM #8Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Chào các bác! E cũng đang tìm làm một project nhỏ với xml nhưng đang mắc phần xóa một node. E có file xml nội dung thế này:
Mã:<?xml version="1.0" encoding="utf-16"?><wfs:FeatureCollection xmlns="http://www.opengis.net/wfs" xmlns:wfs="http://www.opengis.net/wfs" xmlns:new="http://localhost:8080/geoserver" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost:8080/geoserver http://localhost:8080/geoserver/wfs?...e=new:Bao_tang http://www.opengis.net/wfs http://localhost:8080/geoserver/schemas/wfs/1.0.0/WFS-basic.xsd"> <gml:boundedBy> <gml:Box srsName="http://www.opengis.net/gml/srs/epsg.xml#4326"> <gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs="," ts="">105.79407768,20.99910479 105.86126965,21.04643932</gml:coordinates> </gml:Box> </gml:boundedBy> <gml:featureMember> <new:Bao_tang> <new:the_geom> <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326"> <gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs="" ds="">105.85141496,21.02330989</gml:coordinates> </gml:Point> <ew:the_geom> <new:TEN_XE>02<ew:TEN_XE> <new:TEN_LAI_XE>Sinh<ew:TEN_LAI_XE> <new:SDT>0906181284<ew:SDT> <new:DIA_CHI>YB<ew:DIA_CHI> <ew:Bao_tang> </gml:featureMember> <gml:featureMember> <new:Bao_tang> <new:the_geom> <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326"> <gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs="" ds="">105.85141496,21.02330989</gml:coordinates> </gml:Point> <ew:the_geom> <new:TEN_XE>04<ew:TEN_XE> <new:TEN_LAI_XE>Đỗ Tuấn Anh<ew:TEN_LAI_XE> <new:SDT>0902166740<ew:SDT> <new:DIA_CHI>Thanh Xuân<ew:DIA_CHI> <ew:Bao_tang> </gml:featureMember></wfs:FeatureCollection>
Mã:XmlDocument doc = new XmlDocument(); FileStream fstream3 = new FileStream(xmlPath,FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite); try { doc.Load(fstream3); } catch (Exception err) { MessageBox.Show(err.Message); return; } XmlNodeList ftMember = doc.GetElementsByTagName("gml:featureMember"); //MessageBox.Show(ftMember.Count.ToString()); XmlNode rootNode = doc.DocumentElement; //MessageBox.Show(rootNode.InnerXml); for (int i = 0; i < ftMember.Count; i++) { if (ftMember[i].ChildNodes[0].ChildNodes[3].InnerText == cbPhoneNo.SelectedItem.ToString()) { rootNode.RemoveChild(rootNode.ChildNodes[i+1]); //MessageBox.Show(rootNode.ChildNodes[i+1].OuterXml); } } fstream3.Seek(0, SeekOrigin.Begin); fstream3.Flush(); doc.Save(fstream3); fstream3.Close();
Mã:<gml:featureMember> <new:Bao_tang> <new:the_geom> <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326"> <gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs="" ds="">105.85141496,21.02330989</gml:coordinates> </gml:Point> <ew:the_geom> <new:TEN_XE>04<ew:TEN_XE> <new:TEN_LAI_XE>Đỗ Tuấn Anh<ew:TEN_LAI_XE> <new:SDT>0902166740<ew:SDT> <new:DIA_CHI>Thanh Xuân<ew:DIA_CHI> <ew:Bao_tang> </gml:featureMember></wfs:FeatureCollection> <new:Bao_tang> <new:the_geom> <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326"> <gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs="" ds="">105.85141496,21.02330989</gml:coordinates> </gml:Point> <ew:the_geom> <new:TEN_XE>02<ew:TEN_XE> <new:TEN_LAI_XE>Sinh<ew:TEN_LAI_XE> <new:SDT>0906181284<ew:SDT> <new:DIA_CHI>YB<ew:DIA_CHI> <ew:Bao_tang> </gml:featureMembere>
-
26-03-2009, 04:46 PM #9Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Tại sao lại có sự khác biệt khi Save dữ liệu từ bộ nhớ vào trực tiếp file với Save thông qua một Stream nhỉ? Mình thử doc.Save(tên file) thì kết quả đúng nhưng Save(fileStream) thi lại không được. Trong khi yêu cầu là phải save vào Stream vì file đang được open. Bạn nào có cách giải quyết cho vấn đề này không.
-
05-06-2011, 11:54 AM #10Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
childnode.Attributes("employeeid").Name
Rocket 1h là thực phẩm chức năng giúp tăng cường sinh lý nam có nguồn gốc hoàn toàn từ dược liệu tự nhiên. Rất nhiều nam giới đã tuyển lựa Rocket 1h như một phương thuốc hữu hiệu để cải thiện tình...
Nguyên nhân của cặp đôi nam nữ mua...