Khởi Tạo Một Tài Liệu XML Với C#
Các thao tác chính khuyến khích bạn nên tham khảo là: Tạo, xem, thêm, sửa, xóa, tìm kiếm ... Trong .NET hỗ trợ nhiều APIs để làm việc với cấu trúc dữ liệu này bằng nhiều cách khác nhau.A. Sử Dụng DOM Truyền Thống
- Đầu tiên sử dụng class XmlDocument nằm tại namespace System.Xml để tạo một XML file có nội dung như thế này:<Root>
<Node ID="100">
<Name>Tuan Le Minh</Name>
<Country>Viet Nam</Country>
</Node>
</Root>
Lần lượt khởi tạo như sau:
XmlDocument doc = new XmlDocument();
// Create a root element
XmlElement rootname = doc.CreateElement("Root");
// Create a node element
XmlElement drivename = doc.CreateElement("Node");
drivename.SetAttribute("ID","100");
// Create data for node
XmlElement fullname = doc.CreateElement("Name");
fullname.InnerText = "Tuan Le Minh";
XmlElement country = doc.CreateElement("Country");
country.InnerText = "Viet Nam";
// Add data into node
drivename.AppendChild(fullname);
drivename.AppendChild(country);
// Add node into root
rootname.AppendChild(drivename);
// Insert xml into doc
doc.AppendChild(rootname);
// Save file
doc.Save("Test.xml");
Bạn có thể thử mã trên bên trong giao diện console sau đó vào thư mục ../Bin/Debug/ để tìm mẫu xml đã được tạo ra bởi bạn.
- Tiếp theo, chúng ta tạo một tài liệu đầy đủ hơn với declaration, processing instructions, comments. Bạn thêm vào mẫu code bên trên :
XmlDocument doc = new XmlDocument();
// Create new declaration
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
doc.AppendChild(dec);
// Create new Processing Instruction
XmlProcessingInstruction pro = doc.CreateProcessingInstruction("xml-stylesheet",
"href='stenw.css' title='STENW' type='text/css'");
doc.AppendChild(pro);
// Add new comment
XmlComment cmt = doc.CreateComment("Your comment here....");
doc.AppendChild(cmt);
B. Sử Dụng Linq To Xml
- Có 2 thư viện hay được sử dụng là: XDocument vs XElement nằm tại namespace System.Xml.Linq. Bình thường thì bạn có thể dùng cả 2 để tạo một Xml file.- Sự khác biệt ở đây là khi bạn sử dụng XElement nó sẽ đại diện cho một phần tử của Xml tương tự như mẫu Xml bên trên mà chúng ta đã tạo (A)
- Còn đối với XDocument, nó thực sự giúp bạn tạo một tài liệu Xml đầy đủ tiêu chuẩn. Bao gồm các declaration, processing instructions, comments , and element.
a. Với XDocument
- Chúng ta sẽ tạo tài liệu Xml sau:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet href='stenw.css' title='STENW' type='text/css'?>
<!--Comment: follow us on fb: https://www.facebook.com/tuhocit-->
<Root>
<Node ID="100">
<Name>Tuan Le Minh</Name>
<Country>Viet Nam</Country>
</Node>
</Root>
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XProcessingInstruction("xml-stylesheet", "href='stenw.css' title='STENW' type='text/css'"),
new XComment("Comment: follow us on fb: https://www.facebook.com/tuhocit"),
new XElement("Root",
new XElement("Node", new XAttribute("ID",100),
new XElement("Name","Tuan Le Minh" ),
new XElement("Country", "Viet Nam")
)
)
);
doc.Save("Test2.xml");
b. Với XElement
- Ta chỉ có thể tạo mẫu đơn giản với lệnh như sau:
XElement doc = new XElement("Root",
new XElement("Node", new XAttribute("ID",100),
new XElement("Name","Tuan Le Minh" ),
new XElement("Country", "Viet Nam")
)
);
doc.Save("Test3.xml");
<Root>
<Node ID="100">
<Name>Tuan Le Minh</Name>
<Country>Viet Nam</Country>
</Node>
</Root>
(Continued)
Created: 21/04/2014
[XML] How To: Create XML Document In C#
Related Tags :WritingObsolete
No comments:
Post a Comment
Commets Download Photoshop Actions, Lightroom Presets, PSD Template, Mockups, Stocks, Vectors, Fonts. Download free