views:

47

answers:

1

In a new class I'm writing, I need to take the contents of an external XML data feed and convert it into an HTML table via C# and .NET 2.0 (cannot go any higher due to server constraints). Since we reuse the code, I'm putting this functionality into a class. Here is the XML Loader class:

using System;
using System.Web;
using System.Data;
using System.Text;
using System.Diagnostics;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace 
{
    public partial class XMLLoader
    {
        private String xmlpath = "\\xml\\data_test.xml";

    public String getXMLData()
    {
        DataSet dsXMLData = new DataSet("xml_data");
        string filePath = xmlpath;
        dsXMLData.ReadXml(filePath);
        Debug.WriteLine(dsXMLData.GetXml());

        DataGrid DataGrid1 = new DataGrid();
        DataGrid1.AllowPaging = true;
        DataGrid1.DataSource = dsXMLData;
        DataGrid1.DataMember = "xml_data";

        StringBuilder outStr = new  StringBuilder(String.Empty);
        outStr.Append(dsXMLData.GetXml());
        return outStr.ToString();
    }
}
}

Where I am getting stuck is outputting the contents of the data grid to an HTML Table using the outStr var which I am simply writing to the APSX page using <% Response.Write(); %>. Either way I'm stuck and so far not finding what I want to display in MSDN.

A: 

You need to call the method Databind() to your datagrid and add your datagrid to your page controls.

EDIT:

You can also use the method Render(yourHTMLTextWriter) of your datagrid after the binding

Gregoire
Gregoire, thanks.If I have <% Response.Write(getHTMLTable()); %> in my APSX page and protected String getHTMLTable() { XMLLoader xl = new XMLLoader (); return xl.loadXML(); } in my .cs page, how would I get the HTML to the APSX page via HTMLTextWriter?
JFOX
@JFOX: try yourGrid.Render(Response.Output)
Gregoire
Thanks Gregoire, see new comment above.
JFOX