字符串的序列化

字符串的序列化用来处理字符串的交流信息,例如,当我们去把Xml文件中的字符串读取出来,或则把字符串写到Xml文件里面去;又或则,当我们需要写程序与服务器交流,这时候我们要处理json格式的字符串 。这些都需要用到字符串的序列化。这个是很必要的。接下来总结一下常用的字符串序列化知识。

 序列化 知识点
-> 序列化就是格式化,是指将一个对象以某种格式进行呈现
-> 二进制序列化
-> 在需要序列化的类前标记[Serializable]
-> 创建序列化的对象BinaryFormatter
-> 创建流
-> 调用Serialize方法
-> XML序列化
XmlSerialicer
-> JavaScript序列化(JSON格式的数据)
JavaScriptSeralizer

贴代码如下:

Address 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace serializeLearn
{
    //在需要序列化的类前标记[Serializable]
    [Serializable]
    public class Address
    {
        public int Id { get; set; }
        public string Name { get; set; } 
    }
}

main 类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Xml.Serialization;

namespace serializeLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            //二进制序列化
#if false
            //序列化
            string s = "\u4e09\u73af\u4ee5\u5185";
            //创建序列化的对象BinaryFormatter 创建序列化的对象
            BinaryFormatter bf = new BinaryFormatter();
            //创建流
            using (FileStream f = new FileStream("data", FileMode.Create, FileAccess.Write))
            {
                bf.Serialize(f, new Address() { Name = s, Id = 11 });//序列化操作
            }
#endif

            //二进制反序列化
#if false
            BinaryFormatter bf = new BinaryFormatter();
            using (FileStream f = new FileStream("data", FileMode.Open, FileAccess.Read))
            {
                Address a = bf.Deserialize(f) as Address;
                Console.WriteLine("ID="+a.Id);
                Console.WriteLine("Name="+a.Name);
                Console.ReadLine(); 
            }
#endif
            //xml 序列化   
#if false
            Address a = new Address() { Name = "\u4e09\u73af\u4ee5\u5185", Id = 11 };
            XmlSerializer xf = new XmlSerializer(typeof(Address)); // 固定语法
            using (FileStream f = new FileStream("data.xml", FileMode.Create, FileAccess.Write))
            {
                xf.Serialize(f, a);
            }
#endif
            //xml 反序列化  
#if false
            XmlSerializer xf = new XmlSerializer(typeof(Address)); // 固定语法
            using (FileStream f = new FileStream("data.xml", FileMode.Open, FileAccess.Read))
            {
                Address s = xf.Deserialize(f) as Address;
                Console.WriteLine("ID=" + s.Id);
                Console.WriteLine("Name=" + s.Name);
            }   
#endif

            //json 序列化跟反序列化
            //添加一个引用 Web.Extension
            Address a = new Address() { Name = "\u4e09\u73af\u4ee5\u5185", Id = 11 };
            JavaScriptSerializer jf = new JavaScriptSerializer();
            //序列化
            string s = jf.Serialize(a);
            Console.WriteLine("s=" + s);
            //反序列化
            Address aa = jf.Deserialize(s, typeof(Address)) as Address;
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值