ASP.NET中使用Treeview和XML(2)
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>
如果我们使用第一种方法,必须对xml进行重写,用以下的形式表示,才能绑定到树形控件中去:
<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>
增加以下代码:
<form runat="server">
<ie:treeview runat="server">
<ie:treenode runat="server" text="asp.net books" expanded="true" treenodesrc="aspnetbooks.xml" />
</ie:treeview>
</form>
这样就将该xml文件绑定到树形控件中去了,运行后可以看到结果:
asp.net books
teach yourself active server pages 3.0 in 21 days
designing active server pages
asp.net: tips, tutorials, and code
programming asp.net
可以看到,使用第一种方法的确比较麻烦,不能对xml的结点进行筛选或者其他操作。而如果使用第二种方法的xsl,则可以根据需要随时对原来的xml进行格式的控制,十分方便。
Tag: TreeView ,xml