(1)将对象序列化为bin,soap,xml
(1) 在Remoting传递对象需要将对象序列化,本节先学习如何将对象序列化为bin,soap,xml
主要知识点
1. [Serializable]
2. System.Runtime.Serialization.Formatters.Binary;
3. System.Runtime.Serialization.Formatters.Soap;
4. System.Xml.Serialization;
(2) 对象
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
using System.Collections.Generic;
namespace ZhUtility
{
[Serializable]
public class SumOf
{
private DecimalList _member = new DecimalList();
private decimal _sum;
private decimal _avg;
public DecimalList Member
{
get { return _member; }
}
public decimal Sum
{
get { return _sum; }
}
public decimal Avg
{
get { return _avg; }
}
public SumOf()
{
}
public void Calculate()
{
this._sum = 0;
foreach (decimal m in _member)
{
this._sum += m;
}
this._avg = _sum / _member.Count;
}
}
[Serializable]
public class DecimalList : List<decimal>
{
}
}
(3) Test1.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test1.aspx.cs" Inherits="Test1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtNumber" runat="server" Text="10"></asp:TextBox>
<asp:Button ID="btnCreateObj" runat="server" Text="创建对象" OnClick="BtnCreateObj_Click" />
<br /><br />
<asp:Button ID="btnCreateBin" runat="server" Text="bin" Width="60px" OnClick="BtnCreateBin_Click" />
<asp:Button ID="btnCreateSoap" runat="server" Text="soap" Width="60px" OnClick="BtnCreateSoap_Click" />
<asp:Button ID="btnCreateXml" runat="server" Text="xml" Width="60px" OnClick="BtnCreateXml_Click" />
<br /><br />
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
</form>
</body>
</html>
(4) Test1.aspx.cs代码
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
using ZhUtility;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;
public partial class Test1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//创建对象
protected void BtnCreateObj_Click(object sender, EventArgs e)
{
SumOf mySumOf = this.CreateSumOfObj();
lblMessage.Text = "数量:" + mySumOf.Member.Count.ToString() + " 合计:" + mySumOf.Sum.ToString() + " 平均值:" + mySumOf.Avg.ToString();
}
//bin
protected void BtnCreateBin_Click(object sender, EventArgs e)
{
SumOf mySumOf = this.CreateSumOfObj();
FileStream myFileStream = new FileStream(@"G:\DoSum.bin", FileMode.Create);
BinaryFormatter myBinaryFormatter = new BinaryFormatter();
myBinaryFormatter.Serialize(myFileStream, mySumOf);
myFileStream.Close();
}
//soap
protected void BtnCreateSoap_Click(object sender, EventArgs e)
{
SumOf mySumOf = this.CreateSumOfObj();
FileStream myFileStream = new FileStream(@"G:\DoSum_Soap.xml", FileMode.Create);
SoapFormatter mySoapFormatter = new SoapFormatter();
mySoapFormatter.Serialize(myFileStream, mySumOf);
myFileStream.Close();
}
//xml
protected void BtnCreateXml_Click(object sender, EventArgs e)
{
SumOf mySumOf = this.CreateSumOfObj();
FileStream myFileStream = new FileStream(@"G:\DoSum.xml", FileMode.Create);
XmlSerializer myXmlSerializer = new XmlSerializer(typeof(SumOf));
myXmlSerializer.Serialize(myFileStream, mySumOf);
myFileStream.Close();
}
private SumOf CreateSumOfObj()
{
SumOf mySumOf = new SumOf();
for (int i = 0; i < int.Parse(txtNumber.Text); i++)
{
mySumOf.Member.Add(i);
}
mySumOf.Calculate();
return mySumOf;
}
}