zoukankan      html  css  js  c++  java
  • c#中SOAP序列化和反序列化

    c#中SOAP序列化和反序列化

     上代码!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.Serialization;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Soap;//记得引用dll 和他的空间名滴呀; 
    namespace SerlizeSoap
    {
        [Serializable]
        public class Person
        {
            private string _name;
            private bool _sex;
    
            public Person(string name, bool sex)
            {
                this._name = name;
                this._sex = sex;
            }
    
            public override string ToString()
            {
                return "姓名:" + this._name + " 性别:" + (this._sex ? "" : "");
            }
        }
        [Serializable]
        public class Programmer : Person
        {
            private string _language;
            public Programmer(string name, bool sex, string lan)
                : base(name, sex)
            {
                this._language = lan;
            }
    
            public override string ToString()
            {
                return base.ToString() + "	编程语言" + this._language;
            }
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Programmer p = new Programmer("Jack",true,"c#");
                //使用soap序列化对象;
                string fileName = @"d:programmer.xml";
                Stream fStream = new FileStream(fileName,FileMode.Create,FileAccess.ReadWrite);
                SoapFormatter soapFormat = new SoapFormatter();
                soapFormat.Serialize(fStream,p);
    
                //然后我们再反序列化滴呀;
                fStream.Position = 0;
                p = null;
                p = (Programmer)soapFormat.Deserialize(fStream);
                fStream.Close(); //释放非托管资源;
                Console.WriteLine(p);
                Console.ReadLine();
            }
        }
    }

    总结:

    SOAP序列化与二进制序列化的区别是:SOAP序列化不能序列化泛型类型。

  • 相关阅读:
    计算机网络-数据结构-MAC帧头-IP头-TCP头-UDP头
    (考研)java网络编程
    多态(重点:方法的多态性和对象的多态性)
    JZOJ1497 景点中心 题解
    JZOJ1227 Coprime 题解
    JZOJ3966 Sabotage 题解
    JZOJ3056 数字 题解
    JZOJ3054 祖孙询问 题解
    【Luogu P2282】【JZOJ 4906】【NOIP2016提高组复赛】组合数问题 题解
    JZOJ4316【NOIP2015模拟11.5】Isfind 题解
  • 原文地址:https://www.cnblogs.com/mc67/p/5085080.html
Copyright © 2011-2022 走看看