Best approach to bind data from XML to an asp:Table

Discussion in 'ASP .Net' started by Greg Linwood, Dec 1, 2003.

  1. Greg Linwood

    Greg Linwood Guest

    I am wondering what the best approach to binding XML data to an asp:Table
    from the Page_Load event in a code behind module?

    I'm using VB.Net and initially approached this by adding a table to the web
    page from the VS2003 ASP page designer, extracting an XML document from SQL
    Server during Page_Load, then adding asp:TableRows & asp:TableCells one at a
    time / loading the corresponding xml values a cell at a time.

    Is there a better (less code / maintenance) approach?

    Regards,
    Greg Linwood
    SQL Server MVP
    Greg Linwood, Dec 1, 2003
    #1
    1. Advertising

  2. bind it directly to a datagrid, it renders a table as its output
    automatically.

    --
    Regards

    John Timney (Microsoft ASP.NET MVP)
    ----------------------------------------------
    <shameless_author_plug>
    Professional .NET for Java Developers with C#
    ISBN:1-861007-91-4
    Professional Windows Forms
    ISBN: 1861005547
    Professional JSP 2nd Edition
    ISBN: 1861004958
    Professional JSP
    ISBN: 1861003625
    Beginning JSP Web Development
    ISBN: 1861002092
    </shameless_author_plug>
    ----------------------------------------------

    "Greg Linwood" <> wrote in
    message news:#jI0xu#...
    > I am wondering what the best approach to binding XML data to an asp:Table
    > from the Page_Load event in a code behind module?
    >
    > I'm using VB.Net and initially approached this by adding a table to the

    web
    > page from the VS2003 ASP page designer, extracting an XML document from

    SQL
    > Server during Page_Load, then adding asp:TableRows & asp:TableCells one at

    a
    > time / loading the corresponding xml values a cell at a time.
    >
    > Is there a better (less code / maintenance) approach?
    >
    > Regards,
    > Greg Linwood
    > SQL Server MVP
    >
    >
    John Timney \(Microsoft MVP\), Dec 1, 2003
    #2
    1. Advertising

  3. Greg Linwood

    Greg Linwood Guest

    I found an example of how to bind array data to a datagrid, but haven't seen
    anything on how to bind XML directly to a datagrid.

    Are there any examples you could point out for me that bind xml directly to
    datagrid John?

    Or do you have to have a function that iterates the xml & converts it to the
    array for binding?

    Regards,
    Greg Linwood
    SQL Server MVP

    "John Timney (Microsoft MVP)" <> wrote in message
    news:eDl9C3$...
    > bind it directly to a datagrid, it renders a table as its output
    > automatically.
    >
    > --
    > Regards
    >
    > John Timney (Microsoft ASP.NET MVP)
    > ----------------------------------------------
    > <shameless_author_plug>
    > Professional .NET for Java Developers with C#
    > ISBN:1-861007-91-4
    > Professional Windows Forms
    > ISBN: 1861005547
    > Professional JSP 2nd Edition
    > ISBN: 1861004958
    > Professional JSP
    > ISBN: 1861003625
    > Beginning JSP Web Development
    > ISBN: 1861002092
    > </shameless_author_plug>
    > ----------------------------------------------
    >
    > "Greg Linwood" <> wrote in
    > message news:#jI0xu#...
    > > I am wondering what the best approach to binding XML data to an

    asp:Table
    > > from the Page_Load event in a code behind module?
    > >
    > > I'm using VB.Net and initially approached this by adding a table to the

    > web
    > > page from the VS2003 ASP page designer, extracting an XML document from

    > SQL
    > > Server during Page_Load, then adding asp:TableRows & asp:TableCells one

    at
    > a
    > > time / loading the corresponding xml values a cell at a time.
    > >
    > > Is there a better (less code / maintenance) approach?
    > >
    > > Regards,
    > > Greg Linwood
    > > SQL Server MVP
    > >
    > >

    >
    >
    Greg Linwood, Dec 1, 2003
    #3
  4. Greg,

    See below

    --
    Regards

    John Timney (Microsoft ASP.NET MVP)
    ----------------------------------------------
    <shameless_author_plug>
    Professional .NET for Java Developers with C#
    ISBN:1-861007-91-4
    Professional Windows Forms
    ISBN: 1861005547
    Professional JSP 2nd Edition
    ISBN: 1861004958
    Professional JSP
    ISBN: 1861003625
    Beginning JSP Web Development
    ISBN: 1861002092
    </shameless_author_plug>
    ----------------------------------------------

    The XML file (test.xml)

    <Elements>
    <Links>
    <Text>Wrox Press</Text>
    <Link>http://www.wrox.com</Link>
    </Links>
    <Links>
    <Text>Microsoft</Text>
    <Link>http://www.microsoft.com</Link>
    </Links>
    <Links>
    <Text>Amazon</Text>
    <Link>http://www.amazon.com</Link>
    </Links>
    </Elements>


    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.IO" %>

    <html>
    <script runat="server">

    string SortExpression;

    DataView getData(){

    DataSet ds = new DataSet();
    FileStream fs = new FileStream( Server.MapPath("test.xml"),
    FileMode.Open, FileAccess.Read );
    StreamReader reader = new StreamReader( fs );
    ds.ReadXml( reader );
    fs.Close();

    DataView dv = new DataView(ds.Tables[0] );
    dv.Sort=SortExpression;
    return dv;

    }


    void Page_Load(Object sender, EventArgs e)
    {

    ItemsGrid.DataSource=getData();
    ItemsGrid.DataBind();

    }


    void Sort_Grid(object sender, DataGridSortCommandEventArgs e){

    SortExpression = e.SortExpression.ToString();
    ItemsGrid.DataSource = getData();
    ItemsGrid.DataBind();
    }


    </script>

    <body>

    <form runat="server">

    <h3>DataGrid Example</h3>

    <b>Product List</b>

    <asp:DataGrid id="ItemsGrid"
    BorderColor="black"
    BorderWidth="1"
    CellPadding="3"
    HeaderStyle-BackColor="#00aaaa"
    AutoGenerateColumns="true"
    AllowSorting="true"
    OnSortCommand="Sort_Grid"
    runat="server">

    </asp:DataGrid>

    </form>

    </body>
    </html>


    "Greg Linwood" <> wrote in
    message news:eUW$...
    > I found an example of how to bind array data to a datagrid, but haven't

    seen
    > anything on how to bind XML directly to a datagrid.
    >
    > Are there any examples you could point out for me that bind xml directly

    to
    > datagrid John?
    >
    > Or do you have to have a function that iterates the xml & converts it to

    the
    > array for binding?
    >
    > Regards,
    > Greg Linwood
    > SQL Server MVP
    >
    > "John Timney (Microsoft MVP)" <> wrote in message
    > news:eDl9C3$...
    > > bind it directly to a datagrid, it renders a table as its output
    > > automatically.
    > >
    > > --
    > > Regards
    > >
    > > John Timney (Microsoft ASP.NET MVP)
    > > ----------------------------------------------
    > > <shameless_author_plug>
    > > Professional .NET for Java Developers with C#
    > > ISBN:1-861007-91-4
    > > Professional Windows Forms
    > > ISBN: 1861005547
    > > Professional JSP 2nd Edition
    > > ISBN: 1861004958
    > > Professional JSP
    > > ISBN: 1861003625
    > > Beginning JSP Web Development
    > > ISBN: 1861002092
    > > </shameless_author_plug>
    > > ----------------------------------------------
    > >
    > > "Greg Linwood" <> wrote

    in
    > > message news:#jI0xu#...
    > > > I am wondering what the best approach to binding XML data to an

    > asp:Table
    > > > from the Page_Load event in a code behind module?
    > > >
    > > > I'm using VB.Net and initially approached this by adding a table to

    the
    > > web
    > > > page from the VS2003 ASP page designer, extracting an XML document

    from
    > > SQL
    > > > Server during Page_Load, then adding asp:TableRows & asp:TableCells

    one
    > at
    > > a
    > > > time / loading the corresponding xml values a cell at a time.
    > > >
    > > > Is there a better (less code / maintenance) approach?
    > > >
    > > > Regards,
    > > > Greg Linwood
    > > > SQL Server MVP
    > > >
    > > >

    > >
    > >

    >
    >
    John Timney \(Microsoft MVP\), Dec 2, 2003
    #4
  5. Greg Linwood

    Greg Linwood Guest

    Thanks John

    That's what I'm after.

    Regards,
    Greg Linwood
    SQL Server MVP

    "John Timney (Microsoft MVP)" <> wrote in message
    news:...
    > Greg,
    >
    > See below
    >
    > --
    > Regards
    >
    > John Timney (Microsoft ASP.NET MVP)
    > ----------------------------------------------
    > <shameless_author_plug>
    > Professional .NET for Java Developers with C#
    > ISBN:1-861007-91-4
    > Professional Windows Forms
    > ISBN: 1861005547
    > Professional JSP 2nd Edition
    > ISBN: 1861004958
    > Professional JSP
    > ISBN: 1861003625
    > Beginning JSP Web Development
    > ISBN: 1861002092
    > </shameless_author_plug>
    > ----------------------------------------------
    >
    > The XML file (test.xml)
    >
    > <Elements>
    > <Links>
    > <Text>Wrox Press</Text>
    > <Link>http://www.wrox.com</Link>
    > </Links>
    > <Links>
    > <Text>Microsoft</Text>
    > <Link>http://www.microsoft.com</Link>
    > </Links>
    > <Links>
    > <Text>Amazon</Text>
    > <Link>http://www.amazon.com</Link>
    > </Links>
    > </Elements>
    >
    >
    > <%@ Page Language="C#" %>
    > <%@ Import Namespace="System.Data" %>
    > <%@ Import Namespace="System.IO" %>
    >
    > <html>
    > <script runat="server">
    >
    > string SortExpression;
    >
    > DataView getData(){
    >
    > DataSet ds = new DataSet();
    > FileStream fs = new FileStream( Server.MapPath("test.xml"),
    > FileMode.Open, FileAccess.Read );
    > StreamReader reader = new StreamReader( fs );
    > ds.ReadXml( reader );
    > fs.Close();
    >
    > DataView dv = new DataView(ds.Tables[0] );
    > dv.Sort=SortExpression;
    > return dv;
    >
    > }
    >
    >
    > void Page_Load(Object sender, EventArgs e)
    > {
    >
    > ItemsGrid.DataSource=getData();
    > ItemsGrid.DataBind();
    >
    > }
    >
    >
    > void Sort_Grid(object sender, DataGridSortCommandEventArgs e){
    >
    > SortExpression = e.SortExpression.ToString();
    > ItemsGrid.DataSource = getData();
    > ItemsGrid.DataBind();
    > }
    >
    >
    > </script>
    >
    > <body>
    >
    > <form runat="server">
    >
    > <h3>DataGrid Example</h3>
    >
    > <b>Product List</b>
    >
    > <asp:DataGrid id="ItemsGrid"
    > BorderColor="black"
    > BorderWidth="1"
    > CellPadding="3"
    > HeaderStyle-BackColor="#00aaaa"
    > AutoGenerateColumns="true"
    > AllowSorting="true"
    > OnSortCommand="Sort_Grid"
    > runat="server">
    >
    > </asp:DataGrid>
    >
    > </form>
    >
    > </body>
    > </html>
    >
    >
    > "Greg Linwood" <> wrote in
    > message news:eUW$...
    > > I found an example of how to bind array data to a datagrid, but haven't

    > seen
    > > anything on how to bind XML directly to a datagrid.
    > >
    > > Are there any examples you could point out for me that bind xml directly

    > to
    > > datagrid John?
    > >
    > > Or do you have to have a function that iterates the xml & converts it to

    > the
    > > array for binding?
    > >
    > > Regards,
    > > Greg Linwood
    > > SQL Server MVP
    > >
    > > "John Timney (Microsoft MVP)" <> wrote in message
    > > news:eDl9C3$...
    > > > bind it directly to a datagrid, it renders a table as its output
    > > > automatically.
    > > >
    > > > --
    > > > Regards
    > > >
    > > > John Timney (Microsoft ASP.NET MVP)
    > > > ----------------------------------------------
    > > > <shameless_author_plug>
    > > > Professional .NET for Java Developers with C#
    > > > ISBN:1-861007-91-4
    > > > Professional Windows Forms
    > > > ISBN: 1861005547
    > > > Professional JSP 2nd Edition
    > > > ISBN: 1861004958
    > > > Professional JSP
    > > > ISBN: 1861003625
    > > > Beginning JSP Web Development
    > > > ISBN: 1861002092
    > > > </shameless_author_plug>
    > > > ----------------------------------------------
    > > >
    > > > "Greg Linwood" <> wrote

    > in
    > > > message news:#jI0xu#...
    > > > > I am wondering what the best approach to binding XML data to an

    > > asp:Table
    > > > > from the Page_Load event in a code behind module?
    > > > >
    > > > > I'm using VB.Net and initially approached this by adding a table to

    > the
    > > > web
    > > > > page from the VS2003 ASP page designer, extracting an XML document

    > from
    > > > SQL
    > > > > Server during Page_Load, then adding asp:TableRows & asp:TableCells

    > one
    > > at
    > > > a
    > > > > time / loading the corresponding xml values a cell at a time.
    > > > >
    > > > > Is there a better (less code / maintenance) approach?
    > > > >
    > > > > Regards,
    > > > > Greg Linwood
    > > > > SQL Server MVP
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Greg Linwood, Dec 3, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry Rekow
    Replies:
    1
    Views:
    503
    Hermit Dave
    Aug 31, 2004
  2. WJ
    Replies:
    1
    Views:
    342
    Lau Lei Cheong
    Apr 13, 2005
  3. mr2_93
    Replies:
    1
    Views:
    3,580
    mr2_93
    Oct 2, 2005
  4. Frank Lane
    Replies:
    0
    Views:
    318
    Frank Lane
    Oct 8, 2004
  5. Mudcat
    Replies:
    9
    Views:
    412
    Dennis Lee Bieber
    Mar 21, 2006
Loading...

Share This Page