Xem mẫu
- Lập trình trên môi trường Windows
XML
Trần Duy Hoàng
tdhoang@hcmus.edu.vn
- 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
- 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.
- 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
●
- 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
- 3 qui tắc của xml
Quan hệ lồng nhau
Không hợp lệ
●
...
Hợp lệ
●
...
- Các khái niệm
InnerText
3
4
Attribute
- Đọ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"));
- Đọc file xml
Đọc phanso2.xml
3
4
- Đọ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);
- Đọc file xml
Đọc duongtron.xml
- Đọc file xml
Đọc dagiac.xml
- Đọ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); }
- 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");
- Ghi file xml
Ghi phanso2.xml
3
4
- 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");
- Ghi file xml
Ghi duongtron.xml
- Ghi file xml
Ghi dagiac.xml
- 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");
- Thảo luận
03/23/10 Trần Duy Hoàng - tdhoang@fit.hcmus.edu.vn 20/10
nguon tai.lieu . vn