C# 将DataTable对象转换成XML字符串
2021-04-14 11:26
标签:pos return enc string returns mem new 字符串 name /// C# 将DataTable对象转换成XML字符串 标签:pos return enc string returns mem new 字符串 name 原文地址:https://www.cnblogs.com/cc1120/p/8963810.html
/// 将DataTable对象转换成XML字符串
///
/// DataTable对象
///
public static string GetXmlByDataTable(DataTable dt)
{
if (dt != null)
{
MemoryStream ms = null;
XmlTextWriter XmlWt = null;
try
{
ms = new MemoryStream();
//根据ms实例化XmlWt
XmlWt = new XmlTextWriter(ms, Encoding.Unicode);
//获取ds中的数据
dt.WriteXml(XmlWt);
int count = (int)ms.Length;
byte[] temp = new byte[count];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(temp, 0, count);
//返回Unicode编码的文本
UnicodeEncoding ucode = new UnicodeEncoding();
string returnValue = ucode.GetString(temp).Trim();
return returnValue;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
//释放资源
if (XmlWt != null)
{
XmlWt.Close();
ms.Close();
ms.Dispose();
}
}
}
else
{
return "";
}
}
上一篇:C# 中可空类型
文章标题:C# 将DataTable对象转换成XML字符串
文章链接:http://soscw.com/index.php/essay/75644.html