February 5, 2011 by Christoff Truter ASP.NET XML
I recently took the plunge and finally joined stackoverflow.com and I must
say its quite a brilliant site (one of the best programming sites in my opinion) with a few very cleverly thought out
concepts (badges, bounties, reputation etc).
I also managed to successfully hunt down my first bounty
- something that I feel might make for an useful post.
Imagine you've got the following piece of XML:
XML/books.xml
<?xml version="1.0" encoding="utf-8" ?> <authors> <author name="Edsger Wybe Dijkstra"> <books> <book title="A Discipline of Programming" /> <book title="A Method of Programming" /> <book title="EWD316 - A Short Introduction to the Art of Programming" /> </books> </author> <author name="Bjarne Stroustrup"> <books> <book title="Programming: Principles and Practice Using C++" /> <book title="The C++ Programming Language" /> <book title="The Design and Evolution of C++" /> </books> </author> <author name="Christoff Truter"> </author> </authors>
<asp:XmlDataSource runat="server" ID="xmlSource" XPath="authors/author" DataFile="~/XML/books.xml"> </asp:XmlDataSource> <asp:ListView runat="server" ID="lvAuthors" ItemPlaceholderID="divAuthors" DataSourceID="xmlSource"> <LayoutTemplate> <asp:PlaceHolder runat="server" ID="divAuthors"></asp:PlaceHolder> </LayoutTemplate> <ItemTemplate> <div> <h3> <%#XPath("@name") %> </h3> </div> </ItemTemplate> </asp:ListView>
<asp:ListView runat="server" ID="lvAuthors" ItemPlaceholderID="divAuthors" DataSourceID="xmlSource"> <LayoutTemplate> <asp:PlaceHolder runat="server" ID="divAuthors"></asp:PlaceHolder> </LayoutTemplate> <ItemTemplate> <div> <h3> <%#XPath("@name") %> </h3> <asp:ListView runat="server" ID="lvBooks" DataSource='<%#XPathSelect("books/book") %>' ItemPlaceholderID="divBooks"> <LayoutTemplate> <ul> <asp:PlaceHolder runat="server" ID="divBooks"></asp:PlaceHolder> </ul> </LayoutTemplate> <ItemTemplate> <li> <%#XPath("@title") %> </li> </ItemTemplate> <EmptyDataTemplate> No Books </EmptyDataTemplate> </asp:ListView> </div> </ItemTemplate> </asp:ListView>
<asp:Xml ID="Xml1" runat="server" TransformSource="~/XML/books.xslt" DocumentSource="~/XML/books.xml"></asp:Xml>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output encoding="utf-8" method="html" /> <xsl:template match="authors/author"> <div> <h3> <xsl:apply-templates select="@name"/> </h3> </div> </xsl:template> </xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output encoding="utf-8" method="html" /> <xsl:template match="authors/author"> <div> <h3> <xsl:apply-templates select="@name"/> </h3> <xsl:if test="books/book"> <ul> <xsl:apply-templates select="books/book"/> </ul> </xsl:if> <xsl:if test="not(books/book)"> No Books </xsl:if> </div> </xsl:template> <xsl:template match="books/book"> <li> <xsl:apply-templates select="@title"/> </li> </xsl:template> </xsl:stylesheet>
May 8, 2013 by Marcel
Hi! This is a nice article. Thank for sharing your knowledge. There are some other links related to "Binding XML Data to ListView Control in C#". I hope this is a very useful for developers. http://www.mindstick.com/Articles/b065d7e4-e6a5-4d01-b66f-de1400e63916/?Binding%20XML%20Data%20to%20ListView%20Control http://www.c-sharpcorner.com/UploadFile/c5c6e2/binding-xml-data-to-listview-control-dataset-approach/