本文介绍了DataSet.DataTable.DataRow (Single) 到 XML 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在处理的项目中有强类型数据集,我需要将 DataRow 对象从 DataSet(DataSet 中只有 1 个 DataTable)转换为 XML 字符串.我尝试了以下操作,但完全失败:
I have strongly-typed datasets in the project that I am currently working on and I need to convert a DataRow object from the DataSet (only 1 DataTable in the DataSet) to an XML string. I attempted the following with only utter failure:
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;
任何帮助将不胜感激!
谢谢,基思
推荐答案
我在 MSDN 页面的帮助下解决了 Clone() 函数.
I was able to figure it out with the assistance of a MSDN page regarding the Clone() function.
以下代码经过修改,效果很好:
The following code is revised and works great:
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;
这篇关于DataSet.DataTable.DataRow (Single) 到 XML 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!