在DataTable.WriteXML中包含XML声明

时间:2013-04-12 12:37:19

标签: c# datatable

我需要将DataTable转换为XML字符串 我使用以下内容转换它:

  StringWriter sw = new StringWriter();
  tblXml.WriteXml(sw, XmlWriteMode.WriteSchema);
  string result = sw.ToString();

我正在获取XML字符串,但声明部分未来。

<?xml version="1.0" encoding="utf-8"?>

如何加入?

1 个答案:

答案 0 :(得分:1)

使用XmlTextWriterWriteStartDocument()

XmlTextWriter xmlWriter = new XmlTextWriter(outputStream, Encoding.UTF8);
xmlWriter.WriteStartDocument();
tblXml.WriteXml(xmlWriter);
string result = outputStream.ToString();