DataSet.DataTable.DataRow(单个)到XML字符串



我当前正在处理的项目中有强类型数据集,我需要将DataRow对象从DataSet(DataSet中只有1个DataTable)转换为XML字符串。我尝试了以下操作,但完全失败:

string originalXmlString = string.Empty;
DataSet ds = new DataSet();
ds.Tables.Add(this.ObjectDataRow.Table);
ds.Tables[0].ImportRow(this.ObjectDataRow);
using (StringWriter sw = new StringWriter())
{
    ds.Tables[0].WriteXml(sw);                         
    originalXmlString = sw.ToString();
}
req.OriginalDataRow = originalXmlString;

如有任何帮助,我们将不胜感激!

谢谢,Keith

我能够在MSDN页面上找到关于Clone()函数的帮助。

以下代码经过修订,效果良好:

string originalXmlString = string.Empty;
DataSet ds = new DataSet();
//ds.Tables.Add(this.ObjectDataRow.Table);
ds.Tables.Add(this.ObjectDataRow.Table.Clone());
ds.Tables[0].ImportRow(this.ObjectDataRow);
using (StringWriter sw = new StringWriter())
{
    ds.Tables[0].WriteXml(sw);                         
    originalXmlString = sw.ToString();
}
req.OriginalDataRow = originalXmlString;

最新更新