Sewder
Sewder

Reputation: 754

Populate Dataset with columns from XML

Here is what I have so far:

public void CreateObject()
{
    const string ServerURl = "http://services.odata.org/northwind/northwind.svc/Customers";

    DataSet ds = new DataSet();
    DataTable sourcetable = new DataTable();

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ServerURl);

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    richTextBox1.AppendText(response.StatusDescription);
    Stream datastream = response.GetResponseStream();
    StreamReader reader2 = new StreamReader(datastream);

    using (StreamReader mySR = new StreamReader(datastream, Encoding.GetEncoding("iso-8859-1")))
    {
        XmlDocument lgnXml = new XmlDocument();
        lgnXml.Load(mySR);
        XmlNodeReader reader = new XmlNodeReader(lgnXml);

        ds.ReadXml(reader);

        foreach (DataTable table in ds.Tables)
        {
            foreach (DataRow dr in table.Rows)
            {
                sourcetable.Rows.Add(dr.ItemArray);
            }
        }

        dataGridView1.DataSource = sourcetable;
    }
}

private void button1_Click(object sender, EventArgs e)
{
    CreateObject();
}

When I try to run this I get this error: Input array is longer than the number of columns in this table

I'm guessing i have to add column names to the dataset or datatable? Can I do that from XML?

EDIT Here's the response when I run it using codeninja.sj's method

+---+-------------------------------------------------------------+----------------------+
|   |                              id                             |       updated        |
+---+-------------------------------------------------------------+----------------------+
| 1 | http://services.odata.org/northwind/northwind.svc/Customers | 2016-02-15T20:21:21Z |
+---+-------------------------------------------------------------+----------------------

Upvotes: 2

Views: 1354

Answers (1)

codeninja.sj
codeninja.sj

Reputation: 4119

Approach 1: You have to define column-names and its data-type while creating the source data-table

DataTable sourcetable = new DataTable();
sourcetable.Columns.Add("id", typeof(int));
sourcetable.Columns.Add("ColumnName1", typeof(string));
sourcetable.Columns.Add("ColumnName2", typeof(string));

Approach 2: Convert the API response as XML objects; and convert the same as data-set

string ServerURl = "http://localhost:53835/api/values";
DataSet ds = new DataSet();            
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ServerURl);
request.ContentType = "application/xml";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();            
var datastream = response.GetResponseStream();
XmlReader reader = XmlReader.Create(datastream);
ds.ReadXml(reader);
dataGridView1.DataSource = ds.Tables[0]; //ds.Tables["properties"] --> Specify your XML Node Name here

Suggestion: Second approach is better than the first one; Because you can reuse the same code for different API responses

Note: Modify the above code-snippets based on your API response

Upvotes: 4

Related Questions