加引用:
using System;
using System.Data;
using System.IO;
using System.XML;
using System.Text;
// 相应C#代码:
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();
}
}
private DataSet ConvertXMLToDataSet(string XMLData)
{
StringReader stream = null;
XMLTextReader reader = null;
try
{
DataSet XMLDS = new DataSet();
stream = new StringReader(XMLData);
reader = new XMLTextReader(stream);
XMLDS.ReadXML(reader);
return XMLDS;
}
catch (Exception ex)
{
string strTest = ex.Message;
return null;
}
finally
{
if (reader != null)
reader.Close();
}
}