我当前正在处理的项目中有强类型数据集,我需要将 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;
任何帮助将不胜感激!
谢谢, 基思
最佳答案
在有关 Clone() 的 MSDN 页面的帮助下,我能够弄清楚这一点。功能。
以下代码经过修改并且运行良好:
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;
关于xml - DataSet.DataTable.DataRow(单个)到 XML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9135294/