在一个DataSet中添加Table后,如果Table中存在列为空的情况,则GetXml不会把空列返回,这就造成了,在统一处理Table中的数据时,获取相关列的数据错误,
试过好几种方法,都没有达到预期的效果,先来看下DataSet的GetXml方法返回的数据格式
<NewDataSet>
<Table1>
<OID>20002</OID>
<NAME>sfsfsfsf</NAME>
<ISUSED>1</ISUSED>
<TASKTYPE>1</TASKTYPE>
<TASKDATE>2011-11-27</TASKDATE>
<TASKTIME>19:36:15</TASKTIME>
</Table1>
<Table1>
<OID>20001</OID>
<NAME>测试1</NAME>
<ISUSED>1</ISUSED>
<TASKTYPE>1</TASKTYPE>
<TASKDATE>2011-12-27</TASKDATE>
<TASKTIME>16:44:15</TASKTIME>
</Table1>
</NewDataSet>
/// <summary> /// 获取DataSet的Xml格式 /// </summary> /// <param name="tableName">名称 Table1</param> /// <param name="table">DataTable</param> private string GetDataSetXml(string tableName, DataTable table) { string str = string.Empty; for (int i = 0; i < table.Rows.Count; i++) { str += "<" + tableName + ">"; for (int j = 0; j < table.Columns.Count; j++) { string clName = table.Columns[j].ColumnName; str += "<" + clName + ">" + table.Rows[i][clName].ToString() + "</" + clName + ">"; } str += "</" + tableName + ">"; } return str; }