Sử dụng DOM trong .NET
Document Object Model (DOM) thực thi trong .NET hỗ trợ các kĩ thuật W3C DOM
Level 1 và Core DOM Level 2. DOM được thực thi thông qua lớp XmlNode, một lớp ảo
mô tả một nút của tài liệu.
Ngoài ra còn có một lớp XmlNodeList là một danh sách có thứ tự các nút. Đây là một
danh sách sống, và bất kì thay đổi nào cũng sẽ cập nhật ngay vào danh sách.
XmlNodeList hỗ trợ truy cập chỉ mục và truy cập lặp. Còn có một lớp ảo khác,
XmlCharacterData, nó là lớp mở rộng của XmlLinkedNode, và cung cấp các ph
ương
thức sử dụng văn bản cho các lớp khác.
Các lớp XmlNode và XmlNodeList trang điểm cho thực thi DOM trong .NET
Framework. Đây là một danh sách các lớp cơ sở trong XmlNode:
Class Name Description
XmlLinkedNode Trả về nút trước và sau nút hiện tại. Thêm các thuộc tính
NextSibling và PreviousSibling vào XmlNode.
XmlDocument Miêu tả toàn bộ tài liệu. Thực thi các đặc điểm kĩ thuật DOM
Level 1 và Level 2.
XmlDocumentFragment Miêu tả mô hình cây thư mục của tài liệu.
XmlAttribute Một đối tượng thuộc tính của một đối tượng XmlElement.
XmlEntity Phân tách và tổ hợp các nút.
XmlNotation Chứa cách chú thích trong môt DTD hoặc sơ đồ.
Các lớp sau mở rộng XmlCharacterData:
Class Name Description
XmlCDataSection Một đối tượng mô tả một đoạn CData section của một tài
liệu.
XmlComment Mô tả một đối tượng ghi chú XML.
XmlSignificantWhitespace Mô tả một nút với khoảng trắng. Các nút chỉ được tạo
khi cờ PreserveWhiteSpace là true.
XmlWhitespace Miêu tả whitespace trong một thành phần ghi chú. Các
nút được tạo chỉ khi cờ PreserveWhiteSpace là true.
{
// doc is declared at the module level
// change path to match your path structure
doc.Load("..\\..\\..\\books.xml");
// get only the nodes that we want
XmlNodeList nodeLst=doc.GetElementsByTagName("title");
// iterate through the XmlNodeList
foreach(XmlNode node in nodeLst) listBox1.Items.Add(node.InnerText);
}
Chú ý rằng chúng ta cũng có thể thêm khai báo sau vào mô module này:
private XmlDocument doc=new XmlDocument();
Nếu chúng ta chỉ muốn làm như vậy thì dùng XmlReader sẽ tốt hơn vì chỉ cần duyệt qua
tài liệu một lần duy nhất để load dữ liệu cho the listbox. XmlReader được thiết kế cho
mục đích này. Nếu bạn muốn tham chiếu lại một nút, thì dùng XmlDocument là cách tốt
nhất. Mở rộng ví dụ trên bằng cách thêm một quản lí sự kiện khác (đây là
DOMSample2
):
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//create XPath search string
string srch="bookstore/book[title='" + listBox1.SelectedItem.ToString()
+ "']";
//look for the extra data
XmlNode foundNode = doc.SelectSingleNode(srch);
if(foundNode != null)
MessageBox.Show(foundNode.InnerText);
else
MessageBox.Show("Not found");
}
newBook.SetAttribute("publicationdate","2001");
newBook.SetAttribute("ISBN","123456789");
//create a new 'title' element
XmlElement newTitle=doc.CreateElement("title");
newTitle.InnerText="The Case of the Missing Cookie";
newBook.AppendChild(newTitle);
//create new author element
XmlElement newAuthor=doc.CreateElement("author");
newBook.AppendChild(newAuthor);
//create new name element
XmlElement newName=doc.CreateElement("name");
newName.InnerText="C. Monster";
newAuthor.AppendChild(newName);
//create new price element
XmlElement newPrice=doc.CreateElement("price");
newPrice.InnerText="9.95";
newBook.AppendChild(newPrice);
//add to the current document
doc.DocumentElement.AppendChild(newBook);
//write out the doc to disk
XmlTextWriter tr=new XmlTextWriter("..\\..\\..\\booksEdit.xml",null);
tr.Formatting=Formatting.Indented;
doc.WriteContentTo(tr);
tr.Close();
//load listBox1 with all of the titles, including new one
XmlNodeList nodeLst=doc.GetElementsByTagName("title");
foreach(XmlNode node in nodeLst)
listBox1.Items.Add(node.InnerText);
}
Sau khi thực thi mã này, bạn sẽ có như khả năng như ví dụ trên, nhưng ở đây đã thêm