/**////
public class DataToXml { /**////
///
DataTable对象///
public static string CDataToXml(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 ""; } } /**////
///
DataSet对象///
DataSet对象中的Table索引///
public static string CDataToXml(DataSet ds, int tableIndex) { if (tableIndex != -1) { return CDataToXml(ds.Tables[tableIndex]); } else { return CDataToXml(ds.Tables[0]); } } /**////
///
DataSet对象///
public static string CDataToXml(DataSet ds) { return CDataToXml(ds, -1); } /**////
///
DataView对象///
public static string CDataToXml(DataView dv) {
return CDataToXml(dv.Table);
}
/**////
///
DataSet///
XML文件路径///
public static bool CDataToXmlFile(DataTable dt, string xmlFilePath) { if ((dt != null) && (!string.IsNullOrEmpty(xmlFilePath))) { string path = HttpContext.Current.Server.MapPath(xmlFilePath); 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(); //写文件 StreamWriter sw = new StreamWriter(path); sw.WriteLine("
"); sw.WriteLine(ucode.GetString(temp).Trim()); sw.Close(); return true; } catch (System.Exception ex) { throw ex; } finally { //释放资源 if (XmlWt != null) { XmlWt.Close(); ms.Close(); ms.Dispose(); } } } else { return false; } } /**////
///
DataSet对象///
DataSet对象中的Table索引///
xml文件路径///
public static bool CDataToXmlFile(DataSet ds, int tableIndex, string xmlFilePath) { if (tableIndex != -1) { return CDataToXmlFile(ds.Tables[tableIndex], xmlFilePath); } else { return CDataToXmlFile(ds.Tables[0], xmlFilePath); } } /**////
///
DataSet对象///
xml文件路径///
public static bool CDataToXmlFile(DataSet ds, string xmlFilePath) { return CDataToXmlFile(ds, -1, xmlFilePath); } /**////
///
DataView对象///
xml文件路径///
public static bool CDataToXmlFile(DataView dv, string xmlFilePath) { return CDataToXmlFile(dv.Table, xmlFilePath);
}
}
/**////
public class XmlToData { /**////
///
Xml内容字符串///
public static DataSet CXmlToDataSet(string xmlStr) { if (!string.IsNullOrEmpty(xmlStr)) { StringReader StrStream = null; XmlTextReader Xmlrdr = null; try { DataSet ds = new DataSet(); //读取字符串中的信息 StrStream = new StringReader(xmlStr); //获取StrStream中的数据 Xmlrdr = new XmlTextReader(StrStream); //ds获取Xmlrdr中的数据 ds.ReadXml(Xmlrdr); return ds; } catch (Exception e) { throw e; } finally { //释放资源 if (Xmlrdr != null) { Xmlrdr.Close(); StrStream.Close(); StrStream.Dispose(); } } } else { return null; } } /**////
///
Xml字符串///
Table表索引///
public static DataTable CXmlToDatatTable(string xmlStr, int tableIndex) { return CXmlToDataSet(xmlStr).Tables[tableIndex]; } /**////
///
Xml字符串///
public static DataTable CXmlToDatatTable(string xmlStr) { return CXmlToDataSet(xmlStr).Tables[0]; } /**////
///
///
Xml文件地址///
public static DataSet CXmlFileToDataSet(string xmlFilePath) { if (!string.IsNullOrEmpty(xmlFilePath)) { string path = HttpContext.Current.Server.MapPath(xmlFilePath); StringReader StrStream = null; XmlTextReader Xmlrdr = null; try { XmlDocument xmldoc = new XmlDocument();
//根据地址加载Xml文件
xmldoc.Load(path);
DataSet ds = new DataSet(); //读取文件中的字符流 StrStream = new StringReader(xmldoc.InnerXml); //获取StrStream中的数据 Xmlrdr = new XmlTextReader(StrStream); //ds获取Xmlrdr中的数据 ds.ReadXml(Xmlrdr); return ds; } catch (Exception e) { throw e; } finally { //释放资源 if (Xmlrdr != null) { Xmlrdr.Close(); StrStream.Close(); StrStream.Dispose(); } } } else { return null; } } /**////
///
xml文江路径///
Table索引///
public static DataTable CXmlToDataTable(string xmlFilePath, int tableIndex) { return CXmlFileToDataSet(xmlFilePath).Tables[tableIndex]; } /**////
///
xml文江路径///
public static DataTable CXmlToDataTable(string xmlFilePath) { return CXmlFileToDataSet(xmlFilePath).Tables[0];
}
}
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文件SqlConnection conn = new SqlConnection("server=.;database=db;uid=sa;pwd=");SqlCommand cmd = conn.CreateCommand();//把列的内容作为属性,根元素名子为
如果把上面的SqlCommand对象的CommandText改为如下:cmd.CommandText = "select * from student for xml auto,root("root"),elements";执行结果如下: