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

标签 xml c#-4.0 xml-parsing strongly-typed-dataset

我当前正在处理的项目中有强类型数据集,我需要将 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/

相关文章:

xml - 使用 NSXmlParser 解析 ISO-8859-1

java - 如何在 XML 配置中注入(inject) Autowired beans?

xml - F# XML 类型提供程序和可选类型

regex - 如何构建文件名的正则表达式,使其不包含特殊字符且长度有限?

java - android应用程序中的xml解析文件存储在哪里

python - 在Python中使用XPath提取属性值

c# - 为什么具有默认值的枚举在 C# 中不能按预期工作?

java - Java中解析XML的高效方法

java - 为什么我的XML解析器不起作用

xml - 挑战 : Can you make this simple function more elegant using C# 4. 0