惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
Vercel News
Vercel News
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
T
Threatpost
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
I
Intezer
P
Privacy International News Feed
D
Docker
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
Lohrmann on Cybersecurity
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
A
Arctic Wolf
IT之家
IT之家
S
SegmentFault 最新的问题
S
Securelist
博客园 - 叶小钗
N
News and Events Feed by Topic
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - Franky
GbyAI
GbyAI
AI
AI
Y
Y Combinator Blog
WordPress大学
WordPress大学
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
N
News | PayPal Newsroom
The Cloudflare Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
I
InfoQ

博客园 - 小乔的闺房

学习ID,ClientID,UniqueID 基础知识1 (2)最简单的Remoting程序 (4)迭代器 (3)集合接口 (1)学习数组,集合,IEnumerable接口,引申学习迭代器 (2)学习集合,引申学习索引器和泛型 自定义服务器控件(1)整体把握(未完待续) FindControl实现原理 location详解 使用ASP.NET AJAX实现(图片)幻灯片效果 固定GridView列字符串长度,多于的用...代替 读取Excel数据到GridView相关问题(待完善) 说明nchar(10),char(10),nvarchar(10),varchar(10) syscolumns 获得数据库里所有表的名称 类[属性扩展],属性[属性扩展](待完善) 获得数据库表的列数 WebForm里弹出警告框之内的自定义类MessageBox
(1)将对象序列化为bin,soap,xml
小乔的闺房 · 2007-11-04 · via 博客园 - 小乔的闺房

(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;
    }
}