C#实现XML与DataTable互转
Posted 孔小爽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#实现XML与DataTable互转相关的知识,希望对你有一定的参考价值。
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(); } }
以上是关于C#实现XML与DataTable互转的主要内容,如果未能解决你的问题,请参考以下文章
自用 .net C# List集合和DataTable互转,可自定义表头
跳过第一个单元格和最后一个单元格 - 使用 Open XML 从 Excel 到 C# 中的 DataTable