創建可編輯的xml文檔(之一)綁定xml文檔到treeview 控件

- 中國WEB開發者網絡 (http://www.webasp.net)
-- 技術教程 (http://www.webasp.net/article/)
--- 創建可編輯的xml文檔(之一)綁定xml文檔到treeview 控件 (http://www.webasp.net/article/12/11595.htm)
-- 作者:未知
-- 發佈日期: 2004-07-02
目錄:

介紹

綁定xml文檔到treeview 控件

過濾xml 數據

執行拖放操作

執行刪除,改名,插入操作

使用中的treeview 控件



通過xml 和xpath 你可以毫不費力的為你的treeview控件增加拖放甚至更多的功能-by Alex Hildyard



最近,我一直在開發一個用來維護在線目錄的用戶界面工具,因為這個目錄包含太多的產品,所以用一些方法對他們分類是很有意義的。目錄管理員將需要有刪除和定義新的目錄的能力,目錄和目錄之間進行嵌套的能力,還要用巧妙的方式讓目錄和產品看上去很直觀。

像這樣的分類情節迫切需要一個按照種類分類的分等級視圖,第一:在數據和它的表示之間的映射通常很微不足道的(trivial),因為treeview 控件的對象模型是自身分等級的。第二:展開一個獨立的樹節點的能力將用多重級別瀏覽數據變得更容易.最後: 在TreeView中拖放文件夾是快速處理複雜層次非常簡單和吸人注意的方法。

幾分鐘後,我意識到我腦子中的這個應用程序就是Windows Explorer(windows資源管理器), 並且我要重寫它,用產品目錄代替文件夾,用產品項目代替文件,甚至我可以快速的實現類似創建或者刪除文件夾,執行拖放等操作。如果我以後為一個關係數據編寫接口,或者編寫一個聯繫管理程序,或者開發一個追蹤我的家族族譜的工具,那麼我將會發現我做的都是相同的事情。

這麼做是很沒有意義的,我需要找到一個為treeview 控件提供分級數據源的通用方法,這個就好像為一個數據表格控件(data grid)在數據庫創建一個數據表(database table)一樣,並且要能夠很方便的實現創建、刪除、改名、移動、和拖放數據元素的功能,而不需要顧忌詢問中的數據源內容的結構

為treeview 控件創建xml 文檔:

根據treeview的層次機構,xml 是非常合乎邏輯的數據格式,你可以用少於6行的代碼實現在treeview 控件中顯示xml文檔,假設你有一個類似下面這樣的一個xml文檔 ,它包含很多聯繫(contact) 節點:

<?xml version="1.0" encoding="utf-8"?>
<addressbook>
<contacts id="Contacts">
<contact id="Alex">
<email id="popmail">
someone@some_pop_mail.net</email>
<city>Edinburgh</city>
<country>United Kingdom</country>
</contact>
<contact id="Rebekah">
<email id="webmail">
someone@some_web_mail.net</email>
<city>Papakura</city>
<country>New Zealand</country>
</contact>
<contact id="Justin">
<email id="webmail">
someone_else@some_web_mail.com</email>
<city>Muriwai</city>
<country>New Zealand</country>
</contact>
</contacts>
</addressbook>
你可以很容易的通過遞歸調用將所有的數據元素組裝到treeview 控件中,把所有的XML 文檔節點添加到treeview中以後,就可以通過維護treeview控件來維護維護xml文檔的節點關係

[C#]
private void populateTreeControl(
System.Xml.XmlNode document,
System.Windows.Forms.TreeNodeCollection nodes)
{
foreach (System.Xml.XmlNode node in
document.ChildNodes)
{
// If the element has a value, display it;
// otherwise display the first attribute
// (if there is one) or the element name
// (if there isn't)
string text = (node.Value != null ? node.Value :
(node.Attributes != null &&
node.Attributes.Count > 0) ?
node.Attributes[0].Value : node.Name);
TreeNode new_child = new TreeNode(text);
nodes.Add(new_child);
populateTreeControl(node, new_child.Nodes);
}
}



[VB]
Private Sub populateTreeControl( _
ByVal document As System.Xml.XmlNode, _
ByVal nodes As _
System.Windows.Forms.TreeNodeCollection)

Dim node As System.Xml.XmlNode
For Each node In document.ChildNodes
' If the element has a value, display it;
' otherwise display the first attribute
' (if there is one) or the element name
' (if there isn't)
Dim [text] As String
If node.Value <> Nothing Then
[text] = node.Value
Else
If Not node.Attributes Is Nothing And _
node.Attributes.Count > 0 Then
[text] = node.Attributes(0).Value
Else
[text] = node.Name
End If
End If

Dim new_child As New TreeNode([text])
nodes.Add(new_child)
populateTreeControl(node, new_child.Nodes)
Next node
End Sub
現在,你可以新建一個windows窗體,拖放一個treeview 控件到窗體上,添加下面三行到你的數據文件中:

[C#]
System.Xml.XmlDocument document =
new System.Xml.XmlDataDocument();
document.Load("../../contacts.xml");
populateTreeControl(document.DocumentElement,
treeView1.Nodes);



[VB]
Dim document As New System.Xml.XmlDataDocument()
document.Load("../contacts.xml")
populateTreeControl(document.DocumentElement, _
TreeView1.Nodes)
當你展開treeview的節點時,你將看到圖一的內容:



webasp.net