datasetdatasource控件
该控件允许将xml document或其他文件看作dataset进行处理,比如有一个xml文件如下,以books.xml文件命名:
<?xml version="1.0" standalone="yes"?>
<books xmlns="http://tempuri.org/books.xsd">
<book>
<title>asp.net 2.0: a developer's notebook (o'reilly)
</title>
<pubdate>december 2004</pubdate>
<synopsis>to bring you up to speed with asp.net 2.0, this practical book offers nearly 50 hands-on projects.
.</synopsis>
</book>
<book>
<title>.net compact framework pocket guide (o'reilly)
</title>
<pubdate>may 2004</pubdate>
<synopsis>looking to create applications for pocket pc and windows based smartphones? </synopsis>
</book>
</books>
下面,将使用datasetdatasource控件,将xml文件绑定到gridview中。将datasetdatasource控件拖拉到设计窗体,并选“configure data source”,在数据源设置窗体中,选择books.xml作为数据源,再拖拉一个gridview控件,将其绑定到datasetdatasource控件中,运行如下:
xmldatasource控件
该控件也允许将xml document或其他文件绑定到datagrid,gridview中,但被绑定的xml文件的结构可以是不大规则的,不包含dataset。xmldatasource控件还可以使用xpath,可以将xml文件绑定到treeview等其他控件中去。比如一个rss的文件,其xml表示如下,保存为msdn.xml:
拖拉一个xmldatasource控件,点'configure data source…’链接,设置其数据源为msdn.xml,在xpath表达式中,设置为“rss/channel/item”,则只返回item结点下的内容,再拖拉一个datalist控件,将其数据源设置为xmldatasource。
在smart tag菜单中,选择“'auto format…”,并选择slate scheme,再切换到代码窗口,增加如下的代码:
<asp:datalist id="datalist1" runat="server"
gridlines="horizontal"
borderwidth="1px" backcolor="white" cellpadding="3"
borderstyle="none" bordercolor="#e7e7ff"
datasourceid="xmldatasource1">
<footerstyle forecolor="#4a3c8c"
backcolor="#b5c7de"></footerstyle>
<itemtemplate>
<b><%#xpath("title")%></b><br />
<i><%#xpath("pubdate")%></i><br />
<%#xpath("description")%><br />
<a href='<%#xpath("link")%>'>link</a><br />
<br />
</itemtemplate>
<alternatingitemstyle backcolor="#f7f7f7">
</alternatingitemstyle>
<itemstyle forecolor="#4a3c8c"
backcolor="#e7e7ff">
</itemstyle>
<selecteditemstyle forecolor="#f7f7f7"
font-bold="true"
backcolor="#738a9c"></selecteditemstyle>
<headertemplate>rss feeds</headertemplate>
<headerstyle forecolor="#f7f7f7"
font-bold="true"
backcolor="#4a3c8c"></headerstyle>
</asp:datalist>
运行,就可以看到一个简单的rss形式的阅读器了,如下图,十分方便。
至于sitemapdatasource控件,请参考《在asp.net 2.0中使用页面导航控件》一文
总结:
本文简单介绍了asp.net 2.0中十分强大的新增的datasource系列控件,有了这些控件,在与数据库和其他数据源的相关操作中,将十分方便,不用编写太多的代码,在正式版的asp.net 2.0中,估计会增加更多的功能。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器