ASP.Net/XML/XML TreeView
Версия от 15:30, 26 мая 2010; (обсуждение)
Содержание
asp:TreeView binded to XML transformation data
<%--
Code Revised from
Professional ASP.NET 2.0 XML (Programmer to Programmer) (Paperback)
by Thiru Thangarathinam
# Publisher: Wrox (January 18, 2006)
# Language: English
# ISBN: 0764596772
--%>
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Applying XSL Transformation on an XmlDataSource Control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" Runat="server"
DataSourceID="XmlDataSource1" />
<asp:XmlDataSource ID="XmlDataSource1" Runat="server"
DataFile="Bookstore.xml"
TransformFile="Bookstore.xsl" />
</div>
</form>
</body>
</html>
<%--
<bookstore>
<genre name="Fiction">
<book ISBN="10-861003-324" Title="title 1" Price="19.99">
<chapter num="1" name="Introduction">
A
</chapter>
<chapter num="2" name="Body">
B
</chapter>
<chapter num="3" name="Conclusion">
C
</chapter>
</book>
<book ISBN="1-861001-57-5" Title="title " Price="24.95">
<chapter num="1" name="Introduction">
D
</chapter>
<chapter num="2" name="Body">
E
</chapter>
<chapter num="3" name="Conclusion">
F
</chapter>
</book>
</genre>
<genre name="NonFiction">
<book ISBN="10-861003-324" Title="title 2" Price="19.99">
<chapter num="1" name="Introduction">
G
</chapter>
<chapter num="2" name="Body">
H
</chapter>
<chapter num="3" name="Conclusion">
I
</chapter>
</book>
<book ISBN="1-861001-57-6" Title="title 3" Price="27.95">
<chapter num="1" name="Introduction">
J
</chapter>
<chapter num="2" name="Body">
K
</chapter>
<chapter num="3" name="Conclusion">
L
</chapter>
</book>
</genre>
</bookstore>
--%>
<%--
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="bookstore">
<bookstore>
<xsl:apply-templates select="genre"/>
</bookstore>
</xsl:template>
<xsl:template match="genre">
<genre>
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:apply-templates select="book"/>
</genre>
</xsl:template>
<xsl:template match="book">
<book>
<xsl:attribute name="ISBN">
<xsl:value-of select="@ISBN"/>
</xsl:attribute>
<xsl:element name="title">
<xsl:value-of select="title"/>
</xsl:element>
<xsl:element name="price">
<xsl:value-of select="price"/>
</xsl:element>
<xsl:apply-templates select="chapters/chapter" />
</book>
</xsl:template>
<xsl:template match="chapter">
<chapter>
<xsl:attribute name="num">
<xsl:value-of select="@num"/>
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:apply-templates/>
</chapter>
</xsl:template>
</xsl:stylesheet>
--%>
asp TreeView data binding to XML data
<%--
Code Revised from
Professional ASP.NET 2.0 XML (Programmer to Programmer) (Paperback)
by Thiru Thangarathinam
# Publisher: Wrox (January 18, 2006)
# Language: English
# ISBN: 0764596772
--%>
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Binding XML Data from an XmlDataSource Control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" Runat="server" DataSourceID="XmlDataSource1">
<DataBindings>
<asp:TreeNodeBinding ImageUrl="openbook.gif" TextField="Title" DataMember="book"></asp:TreeNodeBinding>
<asp:TreeNodeBinding ImageUrl="notepad.gif" TextField="name" DataMember="chapter"></asp:TreeNodeBinding>
</DataBindings>
</asp:TreeView>
</div>
<div>
<asp:XmlDataSource ID="XmlDataSource1" Runat="server" DataFile="Bookstore.xml"
XPath="bookstore/genre[@name="Fiction"]/book">
</asp:XmlDataSource>
</div>
</form>
XPath="bookstore/genre[@name="Fiction"]/book"</body>
</html>
<%-- Bookstore.xml
<bookstore>
<genre name="Fiction">
<book ISBN="10-861003-324" Title="title 1" Price="19.99">
<chapter num="1" name="Introduction">
A
</chapter>
<chapter num="2" name="Body">
B
</chapter>
<chapter num="3" name="Conclusion">
C
</chapter>
</book>
<book ISBN="1-861001-57-5" Title="title " Price="24.95">
<chapter num="1" name="Introduction">
D
</chapter>
<chapter num="2" name="Body">
E
</chapter>
<chapter num="3" name="Conclusion">
F
</chapter>
</book>
</genre>
<genre name="NonFiction">
<book ISBN="10-861003-324" Title="title 2" Price="19.99">
<chapter num="1" name="Introduction">
G
</chapter>
<chapter num="2" name="Body">
H
</chapter>
<chapter num="3" name="Conclusion">
I
</chapter>
</book>
<book ISBN="1-861001-57-6" Title="title 3" Price="27.95">
<chapter num="1" name="Introduction">
J
</chapter>
<chapter num="2" name="Body">
K
</chapter>
<chapter num="3" name="Conclusion">
L
</chapter>
</book>
</genre>
</bookstore>
--%>
Bind inline XML datasource to asp treeview
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Using Inline XML Data in an XmlDataSource Control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" Runat="server" DataSourceID="XmlDataSource1">
<DataBindings>
<asp:TreeNodeBinding ImageUrl="openbook.gif" TextField="Title" DataMember="book"></asp:TreeNodeBinding>
<asp:TreeNodeBinding ImageUrl="notepad.gif" TextField="name" DataMember="chapter"></asp:TreeNodeBinding>
</DataBindings>
</asp:TreeView>
</div>
<div>
<asp:XmlDataSource ID="XmlDataSource1" Runat="server" XPath="bookstore/book">
<Data>
<bookstore>
<book Title="title 1" Price="19.99">
<chapter num="1" name="Introduction">
A
</chapter>
<chapter num="2" name="Body">
B
</chapter>
<chapter num="3" name="Conclusion">
C
</chapter>
</book>
<book Title="title 2" Price="24.95">
<chapter num="1" name="Introduction">
D
</chapter>
<chapter num="2" name="Body">
E
</chapter>
<chapter num="3" name="Conclusion">
F
</chapter>
</book>
</bookstore>
</Data>
</asp:XmlDataSource>
</div>
</form>
</html>
Build tree out of the XML data file
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>TreeView XML</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView
id="TreeView1"
DataSourceID="srcMovies"
Runat="server" />
<asp:XmlDataSource
id="srcMovies"
DataFile="Movies.xml"
Runat="server" />
</div>
</form>
</body>
</html>
<%--
<?xml version="1.0" encoding="utf-8" ?>
<movies>
<action>
<StarWars />
<IndependenceDay />
</action>
<horror>
<Jaws />
<NightmareBeforeChristmas />
</horror>
</movies>
--%>
detailed XML data binding
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Using XML Data Source</title>
</head>
<body>
<div id="pageContent">
<form id="form1" runat="server">
<asp:xmldatasource runat="server" ID="XmlDataSource1" DataFile="Data.xml">
</asp:xmldatasource>
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1">
<DataBindings>
<asp:TreeNodeBinding Depth="3" DataMember="employeeid"
TextField="#innertext" />
<asp:TreeNodeBinding Depth="3" DataMember="lastname"
TextField="#innertext" />
<asp:TreeNodeBinding Depth="3" DataMember="firstname"
TextField="#innertext" />
<asp:TreeNodeBinding Depth="3" DataMember="title"
TextField="#innertext" />
</DataBindings>
</asp:TreeView>
<hr />
<asp:xmldatasource runat="server" ID="XmlDataSource2" DataFile="Data.xml"
xpath="MyDataSet/NorthwindEmployees/Employee" >
</asp:xmldatasource>
<asp:TreeView ID="TreeView2" runat="server" DataSourceID="XmlDataSource2">
<DataBindings>
<asp:TreeNodeBinding Depth="1" DataMember="employeeid"
TextField="#innertext" />
<asp:TreeNodeBinding Depth="1" DataMember="lastname"
TextField="#innertext" />
<asp:TreeNodeBinding Depth="1" DataMember="firstname"
TextField="#innertext" />
<asp:TreeNodeBinding Depth="1" DataMember="title"
TextField="#innertext" />
</DataBindings>
</asp:TreeView>
</form>
</div>
</body>
</html>
File: Data.xml
<?xml version="1.0" standalone="yes"?>
<MyDataSet>
<NorthwindEmployees>
<Employee>
<employeeid>1</employeeid>
<lastname>D</lastname>
<firstname>N</firstname>
<title>Representative</title>
</Employee>
<Employee>
<employeeid>2</employeeid>
<lastname>Fill</lastname>
<firstname>Andrew</firstname>
<title>President</title>
</Employee>
<Employee>
<employeeid>3</employeeid>
<lastname>Lev</lastname>
<firstname>Janet</firstname>
<title>Sales</title>
</Employee>
</NorthwindEmployees>
</MyDataSet>
TreeNode Binding
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="Data.xml" />
<asp:TreeView DataSourceID="XmlDataSource1"
ID="TreeView1"
ImageSet="WindowsHelp"
runat="server">
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5"
Font-Underline="False"
HorizontalPadding="0px"
VerticalPadding="0px" />
<DataBindings>
<asp:TreeNodeBinding DataMember="book" TextField="Name" />
<asp:TreeNodeBinding DataMember="Publisher" TextField="PublisherName" />
<asp:TreeNodeBinding DataMember="Author" TextField="Name" />
</DataBindings>
<NodeStyle Font-Names="Tahoma"
Font-Size="8pt"
ForeColor="Black"
HorizontalPadding="5px"
NodeSpacing="0px"
VerticalPadding="1px" />
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
</asp:TreeView>
</div>
</form>
</body>
</html>
File: Data.xml
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<book Name="Programming C#">
<Author Name = "AAA" />
<Publisher PublisherName = "publisher 1" />
</book>
<book Name="Programming ASP.NET">
<Author Name = "AAA" />
<Author Name = "Dan Hurwitz" />
<Publisher PublisherName = "publisher 1" />
</book>
<book Name="Visual C# Notebook">
<Author Name = "AAA" />
<Publisher PublisherName = "publisher 1" />
</book>
</Books>
XML data binding?
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Using XML Data Source::Simple</title>
</head>
<body>
<div id="pageContent">
<form id="form1" runat="server">
<asp:xmldatasource runat="server" ID="XmlDataSource1" DataFile="Data.xml" />
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1" />
</form>
</div>
</body>
</html>
File: Data.xml
<?xml version="1.0" standalone="yes"?>
<MyDataSet>
<NorthwindEmployees>
<Employee>
<employeeid>1</employeeid>
<lastname>D</lastname>
<firstname>N</firstname>
<title>Representative</title>
</Employee>
<Employee>
<employeeid>2</employeeid>
<lastname>Fill</lastname>
<firstname>Andrew</firstname>
<title>President</title>
</Employee>
<Employee>
<employeeid>3</employeeid>
<lastname>Lev</lastname>
<firstname>Janet</firstname>
<title>Sales</title>
</Employee>
</NorthwindEmployees>
</MyDataSet>