DataSet通过web service返回xml,两个table能不能嵌套?
如下代码所示:
------------------------------
//该web service的方法返回一个XmlDataDocument对象
public XmlDataDocument TDBinding(string strHostcustid/*用户输入客户号*/)
{
DataSet ds = new DataSet();
XmlDataDocument xd;
DataColumn parentCol;
DataColumn childCol;
//通过ODBC访问数据库的两个表,一个是主表,一个是明细表
OdbcConnection con = new OdbcConnection( "DSN=137 ");
OdbcDataAdapter daCust = new OdbcDataAdapter( "select * from PData1.T01_PARTY where Host_Cust_Id= ' "+strHostcustid + " ' ",con);
OdbcDataAdapter daEvnt = new OdbcDataAdapter( "select * from PData1.t05_financial_event_hm where Host_Cust_Id= ' "+strHostcustid + " ' ",con);
//查询后将两个结果集放入到dataset中,并返回
daCust.Fill(ds, "Customer ");
daEvnt.Fill(ds, "Event ");
xd = new XmlDataDocument(ds);
return xd;
}
这个方法返回的xml格式如下:
<NewDataSet>
<Customer>
......
</Customer>
<Event>
......
</Event>
<Event>
......
</Event>
<Event>
......
</Event>
</NewDataSet>
因为一个Customer会有多个event,这样看起来很长,我希望上面的xml能不能调整成如下格式,就是把event这个结果集再向里缩进一层:
<NewDataSet>
<Customer>
......
</Customer>
<CustomerEvent>
<Event>
......
</Event>
<Event>
......
</Event>
</CustomerEvent>
</NewDataSet>
谢谢。
------解决方案--------------------DataCollumn [] parent = new DataCollumn[1];
DataCollumn [] child = new DataCollumn[1];
parent[0] = ds.Tables( "Customer ").Columns( "Host_Cust_Id ")
child[1] = ds.Tables( "Event ").Columns( "Host_Cust_Id ")
// Create DataRelation.
DataRelation CustEventRel= New DataRelation( "CustomersEvent ", parentCols, childCols, true)
// set the event as the child element of the parent Customer
CustEventRel.Nested = true;
// Add the relation to the DataSet.
ds.Relations.Add(CustEventRel);