1. Trang chủ
  2. » Công Nghệ Thông Tin

Lập trình trên môi trường WindowsXML potx

20 229 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 20
Dung lượng 192,07 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Đọ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 r

Trang 1

Lập trình trên môi trường Windows

XML

Trần Duy Hoàng tdhoang@hcmus.edu.vn

Trang 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

Trang 3

Giới thiệu

 e X tensible M arkup L anguage.

 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.

Trang 4

3 qui tắc của xml

 1 tài liệu xml chỉ có 1 thẻ gốc

<Mang_phan_so>

</Mang_phan_so>

Trang 5

3 qui tắc của xml

 Hệ thống các thẻ đánh dấu

<Phan_so Tu_so="3" Mau_so="4" />

<Tu_so>3</Tu_so>

<Mau_so>4</Mau_so>

</Phan_so>

Trang 6

3 qui tắc của xml

 Quan hệ lồng nhau

<A>

<B> </A>

</B>

<A>

<B> </B>

</A>

Trang 7

Các khái niệm

 InnerText

<Tu_so>3</Tu_so>

<Mau_so>4</Mau_so>

</Phan_so>

 Attribute

<Diem x="3" y="4" />

</Duong_tron>

Trang 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"));

Trang 9

Đọc file xml

 Đọc phanso2.xml

<Tu_so>3</Tu_so>

<Mau_so>4</Mau_so>

</Phan_so>

Trang 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);

Trang 11

Đọc file xml

 Đọc duongtron.xml

<Diem x="3" y="4" />

</Duong_tron>

Trang 12

Đọ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>

Trang 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"));

Trang 14

Ghi file xml

 Ghi phanso1.xml

<Phan_so Tu_so="3" Mau_so="4" />

// 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

Trang 15

Ghi file xml

 Ghi phanso2.xml

<Tu_so>3</Tu_so>

<Mau_so>4</Mau_so>

</Phan_so>

Trang 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

Trang 17

Ghi file xml

 Ghi duongtron.xml

<Diem x="3" y="4" />

</Duong_tron>

Trang 18

Ghi file xml

 Ghi dagiac.xml

<Da_giac>

<Diem x="1" y="2" />

<Diem x="2" y="3" />

<Diem x="3" y="4" />

</Da_giac>

Trang 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());

Trang 20

Thảo luận

Ngày đăng: 08/08/2014, 18:22

TỪ KHÓA LIÊN QUAN