DataSet.DataTable.DataRow (Single) to XML String

﹥>﹥吖頭↗ 提交于 2019-12-06 11:29:39

问题


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;

Any help would be greatly appreciated!

Thanks, Keith


回答1:


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;


来源:https://stackoverflow.com/questions/9135294/dataset-datatable-datarow-single-to-xml-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!