using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// SerializeDataTable 的摘要描述
/// </summary>
public class SerializeDataTable:System.Web.UI.Page
{
public SerializeDataTable()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
/// <summary>
/// 序列化DataTable,将datatable序列化为xml保存在session中,这样做占用服务器资源
/// </summary>
/// <param name="pDt">包含数据的DataTable</param>
/// <returns>序列化的DataTable</returns>
public string SerializeDataTableXml(DataTable pDt)
{
// 序列化DataTable
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
serializer.Serialize(writer, pDt);
writer.Close();
return sb.ToString();
}
/// <summary>
/// 反序列化DataTable
/// </summary>
/// <param name="pXml">序列化的DataTable</param>
/// <returns>DataTable</returns>
public DataTable DeserializeDataTable(string pXml)
{
try
{
StringReader strReader = new StringReader(pXml);
XmlReader xmlReader = XmlReader.Create(strReader);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 可以序列化为xml文件保存起来
/// </summary>
/// <param name="mydt">datatable(必须指定TableName,否则转换失败)</param>
/// <param name="Filename">指定XML文件名</param>
public void SerializeDataTableXmlFile(DataTable mydt,string Filename)
{
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/"+Filename+"_" + Session["UserName"] + ".xml");
try
{
mySerializer.Serialize(myWriter, mydt);
myWriter.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// xml反序列化成datatable
/// </summary>
/// <param name="Filename">XML文件名</param>
/// <returns>datatable</returns>
public DataTable DeserializeXmlFileDataTable(string Filename)
{
DataTable mydt;
// Constructs an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To read the file, creates a FileStream.
FileStream myFileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/" + Filename + "_" + Session["UserName"] + ".xml", FileMode.Open);
// Calls the Deserialize method and casts to the object type.
try
{
mydt = (DataTable)mySerializer.Deserialize(myFileStream);
myFileStream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// 将datatable学列话为二进制文件,并反序列化为datatable
/// 序列化为二进制和xml在需要传输的数据量以及处理该数据所需的 CPU、内存和带宽资源方面降低了一个数量级
/// 这一改进在使用远程处理时有重大意义,但在使用 Web 服务时没有意义,
/// 这是因为 Web 服务按照定义必须传递 XML。这意味着,
/// 只能在通信两端都基于 .NET 并且不是与非 .NET 平台进行通信时,才能利用该增强功能。
///----------------------------------------------------
/// 发现序列化为二进制后,不能編輯和删除,可能二进制文件读写有点问题,此问题待查,咱用xml序列化
/// </summary>
/// <param name="pdt"></param>
public void SerializeDataTableBinary(DataTable mydt)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile_" + Session["UserName"] + ".bin", FileMode.Create, FileAccess.Write, FileShare.None);
try
{
formatter.Serialize(stream, mydt);
stream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
public DataTable DeserializeBinaryDataTable()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
DataTable mydt = (DataTable)formatter.Deserialize(stream);
stream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// SerializeDataTable 的摘要描述
/// </summary>
public class SerializeDataTable:System.Web.UI.Page
{
public SerializeDataTable()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
/// <summary>
/// 序列化DataTable,将datatable序列化为xml保存在session中,这样做占用服务器资源
/// </summary>
/// <param name="pDt">包含数据的DataTable</param>
/// <returns>序列化的DataTable</returns>
public string SerializeDataTableXml(DataTable pDt)
{
// 序列化DataTable
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
serializer.Serialize(writer, pDt);
writer.Close();
return sb.ToString();
}
/// <summary>
/// 反序列化DataTable
/// </summary>
/// <param name="pXml">序列化的DataTable</param>
/// <returns>DataTable</returns>
public DataTable DeserializeDataTable(string pXml)
{
try
{
StringReader strReader = new StringReader(pXml);
XmlReader xmlReader = XmlReader.Create(strReader);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 可以序列化为xml文件保存起来
/// </summary>
/// <param name="mydt">datatable(必须指定TableName,否则转换失败)</param>
/// <param name="Filename">指定XML文件名</param>
public void SerializeDataTableXmlFile(DataTable mydt,string Filename)
{
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/"+Filename+"_" + Session["UserName"] + ".xml");
try
{
mySerializer.Serialize(myWriter, mydt);
myWriter.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// xml反序列化成datatable
/// </summary>
/// <param name="Filename">XML文件名</param>
/// <returns>datatable</returns>
public DataTable DeserializeXmlFileDataTable(string Filename)
{
DataTable mydt;
// Constructs an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To read the file, creates a FileStream.
FileStream myFileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/" + Filename + "_" + Session["UserName"] + ".xml", FileMode.Open);
// Calls the Deserialize method and casts to the object type.
try
{
mydt = (DataTable)mySerializer.Deserialize(myFileStream);
myFileStream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// 将datatable学列话为二进制文件,并反序列化为datatable
/// 序列化为二进制和xml在需要传输的数据量以及处理该数据所需的 CPU、内存和带宽资源方面降低了一个数量级
/// 这一改进在使用远程处理时有重大意义,但在使用 Web 服务时没有意义,
/// 这是因为 Web 服务按照定义必须传递 XML。这意味着,
/// 只能在通信两端都基于 .NET 并且不是与非 .NET 平台进行通信时,才能利用该增强功能。
///----------------------------------------------------
/// 发现序列化为二进制后,不能編輯和删除,可能二进制文件读写有点问题,此问题待查,咱用xml序列化
/// </summary>
/// <param name="pdt"></param>
public void SerializeDataTableBinary(DataTable mydt)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile_" + Session["UserName"] + ".bin", FileMode.Create, FileAccess.Write, FileShare.None);
try
{
formatter.Serialize(stream, mydt);
stream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
public DataTable DeserializeBinaryDataTable()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
DataTable mydt = (DataTable)formatter.Deserialize(stream);
stream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// SerializeDataTable 的摘要描述
/// </summary>
public class SerializeDataTable:System.Web.UI.Page
{
public SerializeDataTable()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
/// <summary>
/// 序列化DataTable,将datatable序列化为xml保存在session中,这样做占用服务器资源
/// </summary>
/// <param name="pDt">包含数据的DataTable</param>
/// <returns>序列化的DataTable</returns>
public string SerializeDataTableXml(DataTable pDt)
{
// 序列化DataTable
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
serializer.Serialize(writer, pDt);
writer.Close();
return sb.ToString();
}
/// <summary>
/// 反序列化DataTable
/// </summary>
/// <param name="pXml">序列化的DataTable</param>
/// <returns>DataTable</returns>
public DataTable DeserializeDataTable(string pXml)
{
try
{
StringReader strReader = new StringReader(pXml);
XmlReader xmlReader = XmlReader.Create(strReader);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 可以序列化为xml文件保存起来
/// </summary>
/// <param name="mydt">datatable(必须指定TableName,否则转换失败)</param>
/// <param name="Filename">指定XML文件名</param>
public void SerializeDataTableXmlFile(DataTable mydt,string Filename)
{
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/"+Filename+"_" + Session["UserName"] + ".xml");
try
{
mySerializer.Serialize(myWriter, mydt);
myWriter.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// xml反序列化成datatable
/// </summary>
/// <param name="Filename">XML文件名</param>
/// <returns>datatable</returns>
public DataTable DeserializeXmlFileDataTable(string Filename)
{
DataTable mydt;
// Constructs an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To read the file, creates a FileStream.
FileStream myFileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/" + Filename + "_" + Session["UserName"] + ".xml", FileMode.Open);
// Calls the Deserialize method and casts to the object type.
try
{
mydt = (DataTable)mySerializer.Deserialize(myFileStream);
myFileStream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// 将datatable学列话为二进制文件,并反序列化为datatable
/// 序列化为二进制和xml在需要传输的数据量以及处理该数据所需的 CPU、内存和带宽资源方面降低了一个数量级
/// 这一改进在使用远程处理时有重大意义,但在使用 Web 服务时没有意义,
/// 这是因为 Web 服务按照定义必须传递 XML。这意味着,
/// 只能在通信两端都基于 .NET 并且不是与非 .NET 平台进行通信时,才能利用该增强功能。
///----------------------------------------------------
/// 发现序列化为二进制后,不能編輯和删除,可能二进制文件读写有点问题,此问题待查,咱用xml序列化
/// </summary>
/// <param name="pdt"></param>
public void SerializeDataTableBinary(DataTable mydt)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile_" + Session["UserName"] + ".bin", FileMode.Create, FileAccess.Write, FileShare.None);
try
{
formatter.Serialize(stream, mydt);
stream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
public DataTable DeserializeBinaryDataTable()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
DataTable mydt = (DataTable)formatter.Deserialize(stream);
stream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// SerializeDataTable 的摘要描述
/// </summary>
public class SerializeDataTable:System.Web.UI.Page
{
public SerializeDataTable()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
/// <summary>
/// 序列化DataTable,将datatable序列化为xml保存在session中,这样做占用服务器资源
/// </summary>
/// <param name="pDt">包含数据的DataTable</param>
/// <returns>序列化的DataTable</returns>
public string SerializeDataTableXml(DataTable pDt)
{
// 序列化DataTable
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
serializer.Serialize(writer, pDt);
writer.Close();
return sb.ToString();
}
/// <summary>
/// 反序列化DataTable
/// </summary>
/// <param name="pXml">序列化的DataTable</param>
/// <returns>DataTable</returns>
public DataTable DeserializeDataTable(string pXml)
{
try
{
StringReader strReader = new StringReader(pXml);
XmlReader xmlReader = XmlReader.Create(strReader);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 可以序列化为xml文件保存起来
/// </summary>
/// <param name="mydt">datatable(必须指定TableName,否则转换失败)</param>
/// <param name="Filename">指定XML文件名</param>
public void SerializeDataTableXmlFile(DataTable mydt,string Filename)
{
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/"+Filename+"_" + Session["UserName"] + ".xml");
try
{
mySerializer.Serialize(myWriter, mydt);
myWriter.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// xml反序列化成datatable
/// </summary>
/// <param name="Filename">XML文件名</param>
/// <returns>datatable</returns>
public DataTable DeserializeXmlFileDataTable(string Filename)
{
DataTable mydt;
// Constructs an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To read the file, creates a FileStream.
FileStream myFileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/" + Filename + "_" + Session["UserName"] + ".xml", FileMode.Open);
// Calls the Deserialize method and casts to the object type.
try
{
mydt = (DataTable)mySerializer.Deserialize(myFileStream);
myFileStream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// 将datatable学列话为二进制文件,并反序列化为datatable
/// 序列化为二进制和xml在需要传输的数据量以及处理该数据所需的 CPU、内存和带宽资源方面降低了一个数量级
/// 这一改进在使用远程处理时有重大意义,但在使用 Web 服务时没有意义,
/// 这是因为 Web 服务按照定义必须传递 XML。这意味着,
/// 只能在通信两端都基于 .NET 并且不是与非 .NET 平台进行通信时,才能利用该增强功能。
///----------------------------------------------------
/// 发现序列化为二进制后,不能編輯和删除,可能二进制文件读写有点问题,此问题待查,咱用xml序列化
/// </summary>
/// <param name="pdt"></param>
public void SerializeDataTableBinary(DataTable mydt)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile_" + Session["UserName"] + ".bin", FileMode.Create, FileAccess.Write, FileShare.None);
try
{
formatter.Serialize(stream, mydt);
stream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
public DataTable DeserializeBinaryDataTable()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
DataTable mydt = (DataTable)formatter.Deserialize(stream);
stream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// SerializeDataTable 的摘要描述
/// </summary>
public class SerializeDataTable:System.Web.UI.Page
{
public SerializeDataTable()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
/// <summary>
/// 序列化DataTable,将datatable序列化为xml保存在session中,这样做占用服务器资源
/// </summary>
/// <param name="pDt">包含数据的DataTable</param>
/// <returns>序列化的DataTable</returns>
public string SerializeDataTableXml(DataTable pDt)
{
// 序列化DataTable
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
serializer.Serialize(writer, pDt);
writer.Close();
return sb.ToString();
}
/// <summary>
/// 反序列化DataTable
/// </summary>
/// <param name="pXml">序列化的DataTable</param>
/// <returns>DataTable</returns>
public DataTable DeserializeDataTable(string pXml)
{
try
{
StringReader strReader = new StringReader(pXml);
XmlReader xmlReader = XmlReader.Create(strReader);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 可以序列化为xml文件保存起来
/// </summary>
/// <param name="mydt">datatable(必须指定TableName,否则转换失败)</param>
/// <param name="Filename">指定XML文件名</param>
public void SerializeDataTableXmlFile(DataTable mydt,string Filename)
{
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/"+Filename+"_" + Session["UserName"] + ".xml");
try
{
mySerializer.Serialize(myWriter, mydt);
myWriter.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// xml反序列化成datatable
/// </summary>
/// <param name="Filename">XML文件名</param>
/// <returns>datatable</returns>
public DataTable DeserializeXmlFileDataTable(string Filename)
{
DataTable mydt;
// Constructs an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To read the file, creates a FileStream.
FileStream myFileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/" + Filename + "_" + Session["UserName"] + ".xml", FileMode.Open);
// Calls the Deserialize method and casts to the object type.
try
{
mydt = (DataTable)mySerializer.Deserialize(myFileStream);
myFileStream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// 将datatable学列话为二进制文件,并反序列化为datatable
/// 序列化为二进制和xml在需要传输的数据量以及处理该数据所需的 CPU、内存和带宽资源方面降低了一个数量级
/// 这一改进在使用远程处理时有重大意义,但在使用 Web 服务时没有意义,
/// 这是因为 Web 服务按照定义必须传递 XML。这意味着,
/// 只能在通信两端都基于 .NET 并且不是与非 .NET 平台进行通信时,才能利用该增强功能。
///----------------------------------------------------
/// 发现序列化为二进制后,不能編輯和删除,可能二进制文件读写有点问题,此问题待查,咱用xml序列化
/// </summary>
/// <param name="pdt"></param>
public void SerializeDataTableBinary(DataTable mydt)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile_" + Session["UserName"] + ".bin", FileMode.Create, FileAccess.Write, FileShare.None);
try
{
formatter.Serialize(stream, mydt);
stream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
public DataTable DeserializeBinaryDataTable()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
DataTable mydt = (DataTable)formatter.Deserialize(stream);
stream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// SerializeDataTable 的摘要描述
/// </summary>
public class SerializeDataTable:System.Web.UI.Page
{
public SerializeDataTable()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
/// <summary>
/// 序列化DataTable,将datatable序列化为xml保存在session中,这样做占用服务器资源
/// </summary>
/// <param name="pDt">包含数据的DataTable</param>
/// <returns>序列化的DataTable</returns>
public string SerializeDataTableXml(DataTable pDt)
{
// 序列化DataTable
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
serializer.Serialize(writer, pDt);
writer.Close();
return sb.ToString();
}
/// <summary>
/// 反序列化DataTable
/// </summary>
/// <param name="pXml">序列化的DataTable</param>
/// <returns>DataTable</returns>
public DataTable DeserializeDataTable(string pXml)
{
try
{
StringReader strReader = new StringReader(pXml);
XmlReader xmlReader = XmlReader.Create(strReader);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 可以序列化为xml文件保存起来
/// </summary>
/// <param name="mydt">datatable(必须指定TableName,否则转换失败)</param>
/// <param name="Filename">指定XML文件名</param>
public void SerializeDataTableXmlFile(DataTable mydt,string Filename)
{
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/"+Filename+"_" + Session["UserName"] + ".xml");
try
{
mySerializer.Serialize(myWriter, mydt);
myWriter.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// xml反序列化成datatable
/// </summary>
/// <param name="Filename">XML文件名</param>
/// <returns>datatable</returns>
public DataTable DeserializeXmlFileDataTable(string Filename)
{
DataTable mydt;
// Constructs an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To read the file, creates a FileStream.
FileStream myFileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/" + Filename + "_" + Session["UserName"] + ".xml", FileMode.Open);
// Calls the Deserialize method and casts to the object type.
try
{
mydt = (DataTable)mySerializer.Deserialize(myFileStream);
myFileStream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// 将datatable学列话为二进制文件,并反序列化为datatable
/// 序列化为二进制和xml在需要传输的数据量以及处理该数据所需的 CPU、内存和带宽资源方面降低了一个数量级
/// 这一改进在使用远程处理时有重大意义,但在使用 Web 服务时没有意义,
/// 这是因为 Web 服务按照定义必须传递 XML。这意味着,
/// 只能在通信两端都基于 .NET 并且不是与非 .NET 平台进行通信时,才能利用该增强功能。
///----------------------------------------------------
/// 发现序列化为二进制后,不能編輯和删除,可能二进制文件读写有点问题,此问题待查,咱用xml序列化
/// </summary>
/// <param name="pdt"></param>
public void SerializeDataTableBinary(DataTable mydt)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile_" + Session["UserName"] + ".bin", FileMode.Create, FileAccess.Write, FileShare.None);
try
{
formatter.Serialize(stream, mydt);
stream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
public DataTable DeserializeBinaryDataTable()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
DataTable mydt = (DataTable)formatter.Deserialize(stream);
stream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// SerializeDataTable 的摘要描述
/// </summary>
public class SerializeDataTable:System.Web.UI.Page
{
public SerializeDataTable()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
/// <summary>
/// 序列化DataTable,将datatable序列化为xml保存在session中,这样做占用服务器资源
/// </summary>
/// <param name="pDt">包含数据的DataTable</param>
/// <returns>序列化的DataTable</returns>
public string SerializeDataTableXml(DataTable pDt)
{
// 序列化DataTable
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
serializer.Serialize(writer, pDt);
writer.Close();
return sb.ToString();
}
/// <summary>
/// 反序列化DataTable
/// </summary>
/// <param name="pXml">序列化的DataTable</param>
/// <returns>DataTable</returns>
public DataTable DeserializeDataTable(string pXml)
{
try
{
StringReader strReader = new StringReader(pXml);
XmlReader xmlReader = XmlReader.Create(strReader);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 可以序列化为xml文件保存起来
/// </summary>
/// <param name="mydt">datatable(必须指定TableName,否则转换失败)</param>
/// <param name="Filename">指定XML文件名</param>
public void SerializeDataTableXmlFile(DataTable mydt,string Filename)
{
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/"+Filename+"_" + Session["UserName"] + ".xml");
try
{
mySerializer.Serialize(myWriter, mydt);
myWriter.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// xml反序列化成datatable
/// </summary>
/// <param name="Filename">XML文件名</param>
/// <returns>datatable</returns>
public DataTable DeserializeXmlFileDataTable(string Filename)
{
DataTable mydt;
// Constructs an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To read the file, creates a FileStream.
FileStream myFileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/" + Filename + "_" + Session["UserName"] + ".xml", FileMode.Open);
// Calls the Deserialize method and casts to the object type.
try
{
mydt = (DataTable)mySerializer.Deserialize(myFileStream);
myFileStream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// 将datatable学列话为二进制文件,并反序列化为datatable
/// 序列化为二进制和xml在需要传输的数据量以及处理该数据所需的 CPU、内存和带宽资源方面降低了一个数量级
/// 这一改进在使用远程处理时有重大意义,但在使用 Web 服务时没有意义,
/// 这是因为 Web 服务按照定义必须传递 XML。这意味着,
/// 只能在通信两端都基于 .NET 并且不是与非 .NET 平台进行通信时,才能利用该增强功能。
///----------------------------------------------------
/// 发现序列化为二进制后,不能編輯和删除,可能二进制文件读写有点问题,此问题待查,咱用xml序列化
/// </summary>
/// <param name="pdt"></param>
public void SerializeDataTableBinary(DataTable mydt)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile_" + Session["UserName"] + ".bin", FileMode.Create, FileAccess.Write, FileShare.None);
try
{
formatter.Serialize(stream, mydt);
stream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
public DataTable DeserializeBinaryDataTable()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
DataTable mydt = (DataTable)formatter.Deserialize(stream);
stream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// SerializeDataTable 的摘要描述
/// </summary>
public class SerializeDataTable:System.Web.UI.Page
{
public SerializeDataTable()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
/// <summary>
/// 序列化DataTable,将datatable序列化为xml保存在session中,这样做占用服务器资源
/// </summary>
/// <param name="pDt">包含数据的DataTable</param>
/// <returns>序列化的DataTable</returns>
public string SerializeDataTableXml(DataTable pDt)
{
// 序列化DataTable
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
serializer.Serialize(writer, pDt);
writer.Close();
return sb.ToString();
}
/// <summary>
/// 反序列化DataTable
/// </summary>
/// <param name="pXml">序列化的DataTable</param>
/// <returns>DataTable</returns>
public DataTable DeserializeDataTable(string pXml)
{
try
{
StringReader strReader = new StringReader(pXml);
XmlReader xmlReader = XmlReader.Create(strReader);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 可以序列化为xml文件保存起来
/// </summary>
/// <param name="mydt">datatable(必须指定TableName,否则转换失败)</param>
/// <param name="Filename">指定XML文件名</param>
public void SerializeDataTableXmlFile(DataTable mydt,string Filename)
{
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/"+Filename+"_" + Session["UserName"] + ".xml");
try
{
mySerializer.Serialize(myWriter, mydt);
myWriter.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// xml反序列化成datatable
/// </summary>
/// <param name="Filename">XML文件名</param>
/// <returns>datatable</returns>
public DataTable DeserializeXmlFileDataTable(string Filename)
{
DataTable mydt;
// Constructs an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To read the file, creates a FileStream.
FileStream myFileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/" + Filename + "_" + Session["UserName"] + ".xml", FileMode.Open);
// Calls the Deserialize method and casts to the object type.
try
{
mydt = (DataTable)mySerializer.Deserialize(myFileStream);
myFileStream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// 将datatable学列话为二进制文件,并反序列化为datatable
/// 序列化为二进制和xml在需要传输的数据量以及处理该数据所需的 CPU、内存和带宽资源方面降低了一个数量级
/// 这一改进在使用远程处理时有重大意义,但在使用 Web 服务时没有意义,
/// 这是因为 Web 服务按照定义必须传递 XML。这意味着,
/// 只能在通信两端都基于 .NET 并且不是与非 .NET 平台进行通信时,才能利用该增强功能。
///----------------------------------------------------
/// 发现序列化为二进制后,不能編輯和删除,可能二进制文件读写有点问题,此问题待查,咱用xml序列化
/// </summary>
/// <param name="pdt"></param>
public void SerializeDataTableBinary(DataTable mydt)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile_" + Session["UserName"] + ".bin", FileMode.Create, FileAccess.Write, FileShare.None);
try
{
formatter.Serialize(stream, mydt);
stream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
public DataTable DeserializeBinaryDataTable()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
DataTable mydt = (DataTable)formatter.Deserialize(stream);
stream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// SerializeDataTable 的摘要描述
/// </summary>
public class SerializeDataTable:System.Web.UI.Page
{
public SerializeDataTable()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
/// <summary>
/// 序列化DataTable,将datatable序列化为xml保存在session中,这样做占用服务器资源
/// </summary>
/// <param name="pDt">包含数据的DataTable</param>
/// <returns>序列化的DataTable</returns>
public string SerializeDataTableXml(DataTable pDt)
{
// 序列化DataTable
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
serializer.Serialize(writer, pDt);
writer.Close();
return sb.ToString();
}
/// <summary>
/// 反序列化DataTable
/// </summary>
/// <param name="pXml">序列化的DataTable</param>
/// <returns>DataTable</returns>
public DataTable DeserializeDataTable(string pXml)
{
try
{
StringReader strReader = new StringReader(pXml);
XmlReader xmlReader = XmlReader.Create(strReader);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 可以序列化为xml文件保存起来
/// </summary>
/// <param name="mydt">datatable(必须指定TableName,否则转换失败)</param>
/// <param name="Filename">指定XML文件名</param>
public void SerializeDataTableXmlFile(DataTable mydt,string Filename)
{
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/"+Filename+"_" + Session["UserName"] + ".xml");
try
{
mySerializer.Serialize(myWriter, mydt);
myWriter.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// xml反序列化成datatable
/// </summary>
/// <param name="Filename">XML文件名</param>
/// <returns>datatable</returns>
public DataTable DeserializeXmlFileDataTable(string Filename)
{
DataTable mydt;
// Constructs an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To read the file, creates a FileStream.
FileStream myFileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/" + Filename + "_" + Session["UserName"] + ".xml", FileMode.Open);
// Calls the Deserialize method and casts to the object type.
try
{
mydt = (DataTable)mySerializer.Deserialize(myFileStream);
myFileStream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// 将datatable学列话为二进制文件,并反序列化为datatable
/// 序列化为二进制和xml在需要传输的数据量以及处理该数据所需的 CPU、内存和带宽资源方面降低了一个数量级
/// 这一改进在使用远程处理时有重大意义,但在使用 Web 服务时没有意义,
/// 这是因为 Web 服务按照定义必须传递 XML。这意味着,
/// 只能在通信两端都基于 .NET 并且不是与非 .NET 平台进行通信时,才能利用该增强功能。
///----------------------------------------------------
/// 发现序列化为二进制后,不能編輯和删除,可能二进制文件读写有点问题,此问题待查,咱用xml序列化
/// </summary>
/// <param name="pdt"></param>
public void SerializeDataTableBinary(DataTable mydt)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile_" + Session["UserName"] + ".bin", FileMode.Create, FileAccess.Write, FileShare.None);
try
{
formatter.Serialize(stream, mydt);
stream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
public DataTable DeserializeBinaryDataTable()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
DataTable mydt = (DataTable)formatter.Deserialize(stream);
stream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// SerializeDataTable 的摘要描述
/// </summary>
public class SerializeDataTable:System.Web.UI.Page
{
public SerializeDataTable()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
/// <summary>
/// 序列化DataTable,将datatable序列化为xml保存在session中,这样做占用服务器资源
/// </summary>
/// <param name="pDt">包含数据的DataTable</param>
/// <returns>序列化的DataTable</returns>
public string SerializeDataTableXml(DataTable pDt)
{
// 序列化DataTable
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
serializer.Serialize(writer, pDt);
writer.Close();
return sb.ToString();
}
/// <summary>
/// 反序列化DataTable
/// </summary>
/// <param name="pXml">序列化的DataTable</param>
/// <returns>DataTable</returns>
public DataTable DeserializeDataTable(string pXml)
{
try
{
StringReader strReader = new StringReader(pXml);
XmlReader xmlReader = XmlReader.Create(strReader);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 可以序列化为xml文件保存起来
/// </summary>
/// <param name="mydt">datatable(必须指定TableName,否则转换失败)</param>
/// <param name="Filename">指定XML文件名</param>
public void SerializeDataTableXmlFile(DataTable mydt,string Filename)
{
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/"+Filename+"_" + Session["UserName"] + ".xml");
try
{
mySerializer.Serialize(myWriter, mydt);
myWriter.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// xml反序列化成datatable
/// </summary>
/// <param name="Filename">XML文件名</param>
/// <returns>datatable</returns>
public DataTable DeserializeXmlFileDataTable(string Filename)
{
DataTable mydt;
// Constructs an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(DataTable));
// To read the file, creates a FileStream.
FileStream myFileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/") + @"/Serialize/" + Filename + "_" + Session["UserName"] + ".xml", FileMode.Open);
// Calls the Deserialize method and casts to the object type.
try
{
mydt = (DataTable)mySerializer.Deserialize(myFileStream);
myFileStream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
/// <summary>
/// 将datatable学列话为二进制文件,并反序列化为datatable
/// 序列化为二进制和xml在需要传输的数据量以及处理该数据所需的 CPU、内存和带宽资源方面降低了一个数量级
/// 这一改进在使用远程处理时有重大意义,但在使用 Web 服务时没有意义,
/// 这是因为 Web 服务按照定义必须传递 XML。这意味着,
/// 只能在通信两端都基于 .NET 并且不是与非 .NET 平台进行通信时,才能利用该增强功能。
///----------------------------------------------------
/// 发现序列化为二进制后,不能編輯和删除,可能二进制文件读写有点问题,此问题待查,咱用xml序列化
/// </summary>
/// <param name="pdt"></param>
public void SerializeDataTableBinary(DataTable mydt)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile_" + Session["UserName"] + ".bin", FileMode.Create, FileAccess.Write, FileShare.None);
try
{
formatter.Serialize(stream, mydt);
stream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
public DataTable DeserializeBinaryDataTable()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("") + @"/Binary/MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
DataTable mydt = (DataTable)formatter.Deserialize(stream);
stream.Close();
return mydt;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex); //拋出異常,在調用處去處理
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// SerializeDataTable 的摘要描述
/// </summary>
public class SerializeDataTable:System.Web.UI.Page
{
public SerializeDataTable()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
%2