怎么由DataTable转化为XML-CSDN论坛
发布网友
发布时间:2023-09-17 13:30
我来回答
共1个回答
热心网友
时间:2024-12-05 18:23
dataTable和dataSet自带转化为xml的功能:
dt.WriteXml(stream);
ds.WriteXml(stream);
示例方法:
private string ConvertDataTableToXML(DataTable xmlDS)
{
MemoryStream stream = null;
XmlTextWriter writer = null;
try
{
stream = new MemoryStream();
writer = new XmlTextWriter(stream, Encoding.Default);
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);
UTF8Encoding utf = new UTF8Encoding();
return utf.GetString(arr).Trim();
}
catch
{
return String.Empty;
}
finally
{
if (writer != null) writer.Close();
}
}