现在的位置: 首页 > 综合 > 正文

创建可编辑的xml文档(之一)绑定xml文档到treeview 控件

2012年11月20日 ⁄ 综合 ⁄ 共 2917字 ⁄ 字号 评论关闭
目录:

介绍

绑定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的节点时,你将看到图一的内容:

抱歉!评论已关闭.