asp.net2.0 GridView Binding XmlDocument

Source: Internet
Author: User
Tags datetime dotnet tostring xmlns xpath
Asp.net|xml

ASP.net 2.0 provides a variety of data sources, in general, the XmlDataSource control uses an XML file, and the following example is a method of data binding using a XmlDocument object.

Vb. NET code

<%@ Page language= "VB"%>
<script runat= "Server" >
Protected Sub Page_Load (ByVal sender as Object, ByVal e as System.EventArgs)
Dim Doc as New System.Xml.XmlDocument
Doc. Load ("http://dotnet.aspx.cc/Rss.aspx")
Xmldatasource1.data = doc. InnerXml
Xmldatasource1.xpath = "/rss/channel/item"
End Sub
</script>
<body>
<form id= "Form1" runat= "Server" >
<asp:gridview id= "GridView1" runat= "Server" datasourceid= "XmlDataSource1" autogeneratecolumns= "false" >
<Columns>
<asp:templatefield headertext= "article title" >
<ItemTemplate>
<asp:hyperlink runat= "Server" target= "_blank" navigateurl= ' <% #XPath ("link")%> ' >
<% #XPath ("title")%></asp:hyperlink>
[<% #CType (XPath ("pubdate"), DateTime). ToString ("yyyy m-month D-day")%>]
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:xmldatasource id= "XmlDataSource1" runat= "Server" ></asp:XmlDataSource>
</form>
</body>

C # code

<%@ Page language= "C #" autoeventwireup= "true"%>
<script runat= "Server" >
protected void Page_Load (object sender, System.EventArgs e)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument ();
Doc. Load ("http://dotnet.aspx.cc/Rss.aspx");
Xmldatasource1.data = doc. INNERXML;
Xmldatasource1.xpath = "/rss/channel/item";
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<asp:gridview id= "GridView1" runat= "Server" datasourceid= "XmlDataSource1" autogeneratecolumns= "false" >
<Columns>
<asp:templatefield headertext= "article title" >
<ItemTemplate>
<asp:hyperlink id= "HyperLink1" runat= "server" target= "_blank" navigateurl= ' <% #XPath ("link")%> ' >
<% #XPath ("title")%></asp:hyperlink>
[<%# (DateTime.Parse (XPath ("pubdate"). ToString (). Replace ("GMT", "")). ToString ("yyyy m-month D-day")%>]
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:xmldatasource id= "XmlDataSource1" runat= "Server" ></asp:XmlDataSource>
</form>
</body>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.