2
這裏列是我到目前爲止有:填充數據集從XML
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();
}
當我試圖運行此我得到這個錯誤: 輸入數組比列數較長此表
我猜我必須添加列名到數據集或數據表?我可以用XML做到嗎?
編輯這裏的響應,當我運行它使用codeninja.sj的方法
+---+-------------------------------------------------------------+----------------------+
| | id | updated |
+---+-------------------------------------------------------------+----------------------+
| 1 | http://services.odata.org/northwind/northwind.svc/Customers | 2016-02-15T20:21:21Z |
+---+-------------------------------------------------------------+----------------------
我嘗試了第二種方法,我只在DataGridView有一行回 – Sewder
@Sewder,可以分享你的API響應嗎? –
我將它添加到問題 – Sewder