首页 > 其他分享 >DataSet(DataTable)与XML互转

DataSet(DataTable)与XML互转

时间:2022-12-13 12:07:25浏览次数:35  
标签:XML stream writer System reader 互转 new null DataTable


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();

}

}

 

标签:XML,stream,writer,System,reader,互转,new,null,DataTable
From: https://blog.51cto.com/u_512327/5933738

相关文章