Bài giảng Lập trình trên môi trường Windows - Chương 8: XML - Trần Duy Hoàng

pdf 20 trang hoanguyen 3130
Bạn đang xem tài liệu "Bài giảng Lập trình trên môi trường Windows - Chương 8: XML - Trần Duy Hoàng", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_giang_lap_trinh_tren_moi_truong_windows_chuong_8_xml_tra.pdf

Nội dung text: Bài giảng Lập trình trên môi trường Windows - Chương 8: XML - Trần Duy Hoàng

  1. Lậ p trình trên môi tr ườ ng Windows XML Tr n Duy Hoàng ầ tdhoang@hcmus.edu.vn
  2. Nộ i dung  Giớ i thi ệ u  3 qui tắ c c ủ a xml  Các khái niệ m  Đọ c file xml  Ghi file xml
  3. Giớ i thi ệ u  eXtensible Markup Language.  Markup language giố ng HTML.  World Wide Web Consortium (W3C)  Đượ c thi ế t k ế đ ể ch ứ a d ữ li ệ u, không ph ả i đ ể hiể n th ị d ữ li ệ u.  Các tags không đượ c đ ị nh nghĩa tr ướ c.
  4. 3 qui tắ c c ủ a xml  1 tài liệ u xml ch ỉ có 1 th ẻ g ố c ● Vi phạ m ● Không vi phạ m
  5. 3 qui tắ c c ủ a xml  Hệ th ố ng các th ẻ đánh d ấ u ● Thẻ r ỗ ng ● Thẻ có ch ứ a th ẻ con 3 4
  6. 3 qui tắ c c ủ a xml  Quan hệ l ồ ng nhau ● Không hợ p l ệ ● Hợ p l ệ
  7. Các khái niệ m  InnerText 3 4  Attribute
  8. Đọ c file xml  Đọ c phanso1.xml // B1 : tao xml document, load noi dung tu tap tin xml XmlDocument doc = new XmlDocument(); doc.Load(" / /phanso1.xml"); // B2 : tao the goc root XmlElement root = doc.DocumentElement; // B3 : lay gia tri trong cac attribute cua root int tuSo = Convert.ToInt32(root.GetAttribute("Tu_so")); int mauSo = Convert.ToInt32(root.GetAttribute("Mau_so"));
  9. Đọ c file xml  Đọ c phanso2.xml 3 4
  10. Đọ c file xml  Đọ c phanso2.xml // B1 : tao xml document, load noi dung tu tap tin xml XmlDocument doc = new XmlDocument(); doc.Load(" / /phanso2.xml"); // B2 : doc the goc root XmlElement root = doc.DocumentElement; // B3 : doc the con Tu_so, Mau_so cua root XmlElement nodeTuSo = (XmlElement)root.SelectSingleNode("Tu_so"); int tuSo = Convert.ToInt32(nodeTuSo.InnerText); XmlElement nodeMauSo = (XmlElement)root.SelectSingleNode("Mau_so"); int mauSo = Convert.ToInt32(nodeMauSo.InnerText);
  11. Đọ c file xml  Đọ c duongtron.xml
  12. Đọ c file xml  Đọ c dagiac.xml
  13. Đọ c file xml  Đọ c dagiac.xml // B1 : tao xml document, load noi dung tu tap tin xml XmlDocument doc = new XmlDocument(); doc.Load(" / /dagiac.xml"); // B2 : doc the goc root XmlElement root = doc.DocumentElement; // B3 : duyet tung node con Diem cua root foreach (XmlElement nodeDiem in root.SelectNodes("Diem")) { CDiem diem = new CDiem(); diem.X = Convert.ToInt32(nodeDiem.GetAttribute("x")); diem.Y = Convert.ToInt32(nodeDiem.GetAttribute("y")); dsDiem.Add(diem); }
  14. Ghi file xml  Ghi phanso1.xml // B1 : tao xml document XmlDocument doc = new XmlDocument(); // B2 : tao the goc root, add root vao doc XmlElement root = doc.CreateElement("Phan_so"); root.SetAttribute("Tu_so", "3"); root.SetAttribute("Mau_so", "4"); doc.AppendChild(root); // B3 : ghi xuong file doc.Save(" / /phanso1.xml");
  15. Ghi file xml  Ghi phanso2.xml 3 4
  16. Ghi file xml  Ghi phanso2.xml // B1 : tao xml document XmlDocument doc = new XmlDocument(); // B2 : tao the goc root, add root vao doc XmlElement root = doc.CreateElement("Phan_so"); doc.AppendChild(root); // B3 : tao nodeTuSo, nodeMauSo, add vao root XmlElement nodeTuSo = doc.CreateElement("Tu_so"); nodeTuSo.InnerText = "3"; root.AppendChild(nodeTuSo);; // B3 : ghi xuong file doc.Save(" / /phanso2.xml");
  17. Ghi file xml  Ghi duongtron.xml
  18. Ghi file xml  Ghi dagiac.xml
  19. Ghi file xml  Ghi dagiac.xml // B1 : tao xml document XmlDocument doc = new XmlDocument(); // B2 : tao the goc root, add root vao doc XmlElement root = doc.CreateElement("Da_giac"); doc.AppendChild(root); // B3 : tao danh sach nodeDiem foreach (CDiem diem in dsDiem) { XmlElement nodeDiem = doc.CreateElement("Diem"); nodeDiem.SetAttribute("x", diem.X.ToString()); nodeDiem.SetAttribute("y", diem.Y.ToString()); root.AppendChild(nodeDiem); } // B4 : ghi xuong file doc.Save(" / /dagiac.xml");
  20. Thả o lu ậ n 02/15/11 Tr n Duy Hoàng - tdhoang@fit.hcmus.edu.vn 20/10 ầ