以上是在设计时,静态添加数据到树形控件的方法。而由于XML实质上也是以树形结构来表示数据的结构,因此,就可以通过使用XML文件绑定到树形控件的方法,来动态加载数据到控件中去,其中有两种方法可以实现:
1)另外写一个符合TREEVIEW格式的XML文件
2)通过XSL将XML进行转换。
先来看下第一种方法,建一个XML文件作为例子,命名为aspnetbooks.xml:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book price="34.95">
<title>Teach Yourself Active Server Pages 3.0 in 21 Days</title>
<authors>
<author>Mitchell</author>
<author>Atkinson</author>
</authors>
<year>1999</year>
</book>
<book price="29.95">
<title>Designing Active Server Pages</title>
<authors>
<author>Mitchell</author>
</authors>
<year>2000</year>
</book>
<book price="34.95">
<title>ASP.NET: Tips, Tutorials, and Code</title>
<authors>
<author>Mitchell</author>
<author>Mack</author>
<author>Walther</author>
<author>Seven</author>
<author>Anders</author>
<author>Nathan</author>
<author>Wahlin</author>
</authors>
<year>2001</year>
</book>
<book price="24.95">
<title>ASP Unleashed</title>
<authors>
<author>Walther</author>
</authors>
<year>1998</year>
</book>
</books>
<books>
<book price="34.95">
<title>Teach Yourself Active Server Pages 3.0 in 21 Days</title>
<authors>
<author>Mitchell</author>
<author>Atkinson</author>
</authors>
<year>1999</year>
</book>
<book price="29.95">
<title>Designing Active Server Pages</title>
<authors>
<author>Mitchell</author>
</authors>
<year>2000</year>
</book>
<book price="34.95">
<title>ASP.NET: Tips, Tutorials, and Code</title>
<authors>
<author>Mitchell</author>
<author>Mack</author>
<author>Walther</author>
<author>Seven</author>
<author>Anders</author>
<author>Nathan</author>
<author>Wahlin</author>
</authors>
<year>2001</year>
</book>
<book price="24.95">
<title>ASP Unleashed</title>
<authors>
<author>Walther</author>
</authors>
<year>1998</year>
</book>
</books>
如果我们使用第一种方法,必须对XML进行重写,用以下的形式表示,才能绑定到树形控件中去:
<TREENODES>
<treenode text="...">
<treenode text="...">
</treenode>
<treenode text="..." />...</TREENODES>
<treenode text="...">
<treenode text="...">
</treenode>
<treenode text="..." />...</TREENODES>
就是说,根结点必须是treenodes(大小写都无所谓),每个子结点必须以<treenode>的形式排列。于是,我们对原来的XML文件改写为如下的形式:
<?xml version="1.0" encoding="UTF-8"?>
<TREENODES>
<treenode text="Teach Yourself Active Server_u80 ?ages 3.0 in 21 Days">
<treenode text="Price - $34.95" />
<treenode text="Authors">
<treenode text="Mitchell" />
<treenode text="Atkinson" />
</treenode>
<treenode text="Year Published - 2000" />
</treenode>
<treenode text="Designing Active Server Pages">
<treenode text="Price - $29.95" />
<treenode text="Authors">
<treenode text="Mitchell" />
</treenode>
<treenode text="Year Published - 2000" />
</treenode>
〈/TREENODES>
<TREENODES>
<treenode text="Teach Yourself Active Server_u80 ?ages 3.0 in 21 Days">
<treenode text="Price - $34.95" />
<treenode text="Authors">
<treenode text="Mitchell" />
<treenode text="Atkinson" />
</treenode>
<treenode text="Year Published - 2000" />
</treenode>
<treenode text="Designing Active Server Pages">
<treenode text="Price - $29.95" />
<treenode text="Authors">
<treenode text="Mitchell" />
</treenode>
<treenode text="Year Published - 2000" />
</treenode>
〈/TREENODES>
这样就将该xml文件绑定到树形控件中去了,运行后可以看到结果:
ASP.NET Books Teach Yourself Active Server Pages 3.0 in 21 DaysDesigning Active Server PagesASP.NET: Tips, Tutorials, and CodeProgramming ASP.NET