我正在使用此代码从此odata uri中提取xml调用。代码拉取xml并将其popular到数据集中。我尝试使用数据集中的所有表填充数据表,如下所示:
Destinations
问题是datagridview被填充了各种搞砸了。列名称与XML结构不匹配。它看起来像这样:
public void CreateObject()
{
const string ServerURl = "http://services.odata.org/northwind/northwind.svc/Customers";
int i =0;
DataSet ds = new DataSet();
DataTable sourcetable = new DataTable();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ServerURl);
request.ContentType = "application/xml";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
richTextBox1.AppendText(response.StatusDescription);
var datastream = response.GetResponseStream();
XmlReader reader = XmlReader.Create(datastream);
ds.ReadXml(reader);
//dataGridView1.DataSource = ds.Tables[0];
foreach (DataTable table in ds.Tables)
{
foreach (DataColumn dc in table.Columns)
{
if (sourcetable.Columns.Contains(dc.ToString().Trim()) == false)
// MessageBox.Show(dc.ToString());
{
sourcetable.Columns.Add(dc.ToString().Trim(),typeof(string));
}
}
}
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
sourcetable.Rows.Add(dr.ItemArray);
}
}
dataGridView1.DataSource = sourcetable;
}
这是XML的片段
+---+---+----------------------+-----------------------------------------+--------------------------------+
| | ID| Feedid | updated | type |
+---+---+----------------------+-----------------------------------------+--------------------------------+
| 1 | | | | |
| 2 | 1 | 0 | 2016-02-15T19:04:01Z | |
| 3 | 2 | Customers | 0 | |
| 4 | 3 | Customers | Customers | |
| 5 | 4 | Customer | Customers('ALFKI') | |
| 6 | 5 | Orders | Customers('ALFKI')/Orders | application/atom+xml;type=feed |
| 7 | 6 | CustomerDemographics | Customers('ALFKI')/CustomerDemographics | application/atom+xml;type=feed |
+---+---+----------------------+-----------------------------------------+--------------------------------+