xml与dataset(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();
}
}
相关文档:
本文系转载,谨向转载处空间拥有者及源出处文章作者表示感谢!
转载处:http://henry19890701.javaeye.com/blog/481462
源出处:http://www.ziliaonet.com/tech/netprogramme/XML/200605/69398.html
在做一般的XML数据交换过程中,我更乐意传递XML字符串,而不是格式化的XML Document。这就涉及到XML字符串和Xml Docume ......
[System.Runtime.Serialization.DataMemberAttribute()]
public Information Archive {
get {
&n ......
已知有一个XML文件(bookstore.xml)如下:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price> ......
/// <summary>
/// 收到的XML转成dataset型
/// </summary>
/// <param name="xmlData"></param>
/// ......