填充数据集从XML列 [英] Populate Dataset with columns from XML
问题描述
下面是我到目前为止有:
公共无效的CreateObject()
{
常量字符串SERVERURL =http://services.odata.org/northwind/northwind.svc/Customers; DataSet的DS =新的DataSet();
数据表sourceTable会=新的DataTable(); HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(SERVERURL); HttpWebResponse响应=(HttpWebResponse)request.GetResponse();
richTextBox1.AppendText(response.StatusDescription);
流数据流= response.GetResponseStream();
StreamReader的reader2 =新的StreamReader(数据流); 使用(StreamReader的mySR =新的StreamReader(数据流,Encoding.GetEncoding(ISO-8859-1)))
{
XmlDocument的lgnXml =新的XmlDocument();
lgnXml.Load(mySR);
XmlNodeReader对象读卡器=新的XmlNodeReader对象(lgnXml); ds.ReadXml(读卡器); 的foreach(在ds.Tables DataTable的表)
{
的foreach(在table.Rows的DataRow博士)
{
sourcetable.Rows.Add(dr.ItemArray);
}
} dataGridView1.DataSource = sourceTable会;
}
}私人无效的button1_Click(对象发件人,EventArgs的发送)
{
的CreateObject();
}
当我尝试运行此我得到这个错误:
输入阵列比列数再在此表
我猜我一定要列名添加到数据集或数据表?我可以这样做,从XML?
编辑下面是当我运行它使用响应codeninja.sj法
<$p$p><$c$c>+---+-------------------------------------------------------------+----------------------+| | ID |更新|
+ --- + --------------------------------------------- ---------------- + ---------------------- +
| 1 | http://services.odata.org/northwind/northwind.svc/Customers | 2016-02-15T20:21:21Z |
+ --- + --------------------------------------------- ---------------- + ----------------------
方法1:您必须定义列名,并在创建源数据表的数据类型
的DataTable sourceTable会=新的DataTable();
sourcetable.Columns.Add(ID的typeof(INT));
sourcetable.Columns.Add(ColumnName1,typeof运算(字符串));
sourcetable.Columns.Add(ColumnName2,typeof运算(字符串));
方法二:转换为XML对象的API响应;并转换相同数据集
字符串SERVERURL =HTTP://本地主机:53835 / API /值;
DataSet的DS =新的DataSet();
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(SERVERURL);
request.ContentType =application / xml进行;
HttpWebResponse响应=(HttpWebResponse)request.GetResponse();
变种数据流= response.GetResponseStream();
读者的XmlReader = XmlReader.Create(数据流);
ds.ReadXml(读卡器);
dataGridView1.DataSource = ds.Tables [0]; //ds.Tables[\"properties] - &GT;在此指定XML节点名称
建议:第二种方法是比第一个好,因为你可以重复使用相同code不同的API响应
注:的修改根据您的API响应上述code-片段
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 |
+---+-------------------------------------------------------------+----------------------
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
这篇关于填充数据集从XML列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!