XML to GridView

Last post 05-19-2008 10:51 AM by anthonypj. 5 replies.

Sort Posts:

  • XML to GridView

    05-15-2008, 11:07 AM
    • Loading...
    • cemundo
    • Joined on 08-21-2006, 10:27 AM
    • Posts 45

    Hi folks.

     I want to populate a Gridview with following xml data (first local xml file, later from web)

     

    <XTC>
    <PAGE>OFFERS</PAGE>

    <REGION>Name der Region</REGION>
    <LCODE>TR</LCODE>
    <LNAME>Region</LNAME>


    <OFFERS>1 - 7 of 7</OFFERS>
    <BACK>1</BACK>


    <OFFER>
    <HOTEL>Hilton Paris</HOTEL>
    <CITY>Paris</CITY>
    <DAYS>7</DAYS>
    <PREIS>563</PREIS>
    </OFFER>

    <OFFER>
    <HOTEL>Hilton Paris</HOTEL>
    <CITY>Paris</CITY>
    <DAYS>7</DAYS>
    <PREIS>603</PREIS>
    </OFFER>

    <OFFER>
    <HOTEL>Hilton Paris</HOTEL>
    <CITY>Paris</CITY>
    <DAYS>5</DAYS>
    <PREIS>609</PREIS>
    </OFFER>
    </XTC>

     I want the gridview to be populates with the childs of <OFFER>. How to do so?

    Thx in advance...

     Cem
     

  • Re: XML to GridView

    05-15-2008, 11:35 AM
    • Loading...
    • ArminStockner
    • Joined on 09-19-2006, 6:18 AM
    • Germany, Bavaria
    • Posts 251

    I dont know how you create the xml file. The easiest way to write and read an xml file to a gridview is to use a method of the database class.

    database.writexml(...)

    database.readxml

     

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: XML to GridView

    05-15-2008, 1:58 PM
    • Loading...
    • cemundo
    • Joined on 08-21-2006, 10:27 AM
    • Posts 45

     the xml already exists (i.e. offers.xml)... the question is how to populate the gridview.

  • Re: XML to GridView

    05-16-2008, 10:39 AM
    • Loading...
    • ArminStockner
    • Joined on 09-19-2006, 6:18 AM
    • Germany, Bavaria
    • Posts 251

    You could try this code:

    DataSet dataSet = new DataSet();
    dataSet.ReadXml("~/App_Data/yourxmlfilename.xml");
    this.GridView1.DataMember = "offer";
    this.GridView1.DataSource = dataSet;
    this.GridView1.DataBind();

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: XML to GridView

    05-16-2008, 10:43 AM
    • Loading...
    • ArminStockner
    • Joined on 09-19-2006, 6:18 AM
    • Germany, Bavaria
    • Posts 251

    Or you can use LINQ to XML

    http://petermcg.wordpress.com/category/linq-to-xml/ 

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: XML to GridView

    05-19-2008, 10:51 AM
    Answer
    • Loading...
    • anthonypj
    • Joined on 05-11-2007, 11:31 AM
    • Posts 274

    the following works

    <%@ Page Language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="XmlDataSource1">

    <Columns>

    <asp:BoundField DataField="HOTEL" HeaderText="HOTEL" SortExpression="HOTEL" />

    <asp:BoundField DataField="CITY" HeaderText="CITY" SortExpression="CITY" />

    <asp:BoundField DataField="DAYS" HeaderText="DAYS" SortExpression="DAYS" />

    <asp:BoundField DataField="PREIS" HeaderText="PREIS" SortExpression="PREIS" />

    </Columns>

    </asp:GridView>

    <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/YourXMLFile.xml" TransformFile="~/XSLTFile.xsl">

    </asp:XmlDataSource>

     

    </div>

    </form>

    </body>

    </html>

    ------ XSLTFile.xsl ----- 

    <?
    xml version="1.0" encoding="utf-8"?>

    <xsl:stylesheet version="1.0"

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes"/>

     

    <xsl:template match ="/">

    <root>

    <xsl:apply-templates select ="//OFFER"/>

    </root>

    </xsl:template>

    <xsl:template match ="//OFFER">

    <OFFER>

    <xsl:attribute name="HOTEL">

    <xsl:value-of select="HOTEL"/>

    </xsl:attribute>

    <xsl:attribute name="CITY">

    <xsl:value-of select="CITY"/>

    </xsl:attribute>

    <xsl:attribute name="DAYS">

    <xsl:value-of select="DAYS"/>

    </xsl:attribute>

    <xsl:attribute name="PREIS">

    <xsl:value-of select="PREIS"/>

    </xsl:attribute>

    </OFFER>

    </xsl:template> </xsl:stylesheet>

     

Page 1 of 1 (6 items)
Microsoft Communities
Page view counter