Professional Documents
Culture Documents
08 XML
08 XML
XML
Trn Duy Hong tdhoang@hcmus.edu.vn
Ni dung
Gii thiu 3 qui tc ca xml Cc khi nim c file xml Ghi file xml
Gii thiu
eXtensible Markup Language. Markup language ging HTML. World Wide Web Consortium (W3C) c thit k cha d liu, khng phi hin th d liu. Cc tags khng c nh ngha trc.
3 qui tc ca xml
1 ti liu xml ch c 1 th gc
Vi phm
<Phan_so Tu_so="3" Mau_so="4" /> <Phan_so Tu_so="4" Mau_so="5" /> <Phan_so Tu_so="5" Mau_so="6" />
Khng vi phm
<Mang_phan_so> <Phan_so Tu_so="3" Mau_so="4" /> <Phan_so Tu_so="4" Mau_so="5" /> <Phan_so Tu_so="5" Mau_so="6" /> </Mang_phan_so>
3 qui tc ca xml
H thng cc th nh du
3 qui tc ca xml
Khng hp l
Hp l
Cc khi nim
InnerText
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
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
<Da_giac> <Diem x="1" y="2" /> <Diem x="2" y="3" /> <Diem x="3" y="4" /> </Da_giac>
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 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 phanso2.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 duongtron.xml
Ghi dagiac.xml
<Da_giac> <Diem x="1" y="2" /> <Diem x="2" y="3" /> <Diem x="3" y="4" /> </Da_giac>
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");
Tho lun
02/15/11
20/10