(1)将对象序列化为bin,soap,xml

本文介绍了一个.NET中的简单示例,展示了如何使用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;
    }
}

转载于:https://www.cnblogs.com/miclu/archive/2007/11/04/948892.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值