DataSet.DataTable.DataRow(单个)到XML字符串 [英] DataSet.DataTable.DataRow (Single) to XML String

查看:138
本文介绍了DataSet.DataTable.DataRow(单个)到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;

任何帮助将不胜感激!

谢谢,
基思(Keith)

Thanks, Keith

推荐答案

在MSDN页面上,关于<一个href = http://msdn.microsoft.com/zh-cn/library/system.data.datatable.clone.aspx rel = nofollow noreferrer> 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(单个)到XML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆