c#序列化

來源:互聯網
上載者:User

標籤:des   http   io   ar   os   使用   sp   for   檔案   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;

namespace LetMeShow
{
class Program
{
static void Main(string[] args)
{

//
Console.WriteLine("let me test it....");
Person p = new Person();
p.way();

}
}

[Serializable]
public class Person
{

public string Sno { get; set; }

public string Name { get; set; }

public string Sex { get; set; }

public int Age { set; get; }

public string DisplayInfo()
{
return "我的學號是:" + Sno + "\n 我的名字是:" + Name+
"\n我的性別為:"+Sex+"\n我的年齡是:"+Age+"\n";

}

//使用binaryFormatter 方式進行序列化..
public void way()
{
var me = new Person
{
Sno = "200717",
Name = "yuanyuan",
Sex = "man",
Age = 22

};

//建立一個格式程式執行個體...
//對象空間的引用的呀..
//using System.Runtime.Serialization.Formatters.Binary;
//using System.Runtime.Serialization;
IFormatter formatter = new BinaryFormatter();
//建立一個檔案流...
//相當與是寫成了一個txt檔案了...
Stream stream = new FileStream(@"E:\xx\序列化", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
formatter.Serialize(stream,me);
stream.Close();

//我在此表示,跳過此章節.....
}

public void returnList()
{
IFormatter formatter = new BinaryFormatter();
Stream destream = new FileStream("",FileMode.Open,FileAccess.Read,FileShare.Read);
var stillme = (Person)formatter.Deserialize(destream);
//這樣就實現了我們的還原序列化的東西....
destream.Close();
}


//方式二
//IFormatter formatter=new SoapFormatter(); 並引用程式集...
//還要引用dll
//system runtime.serialzition.Formatters.soap.dll..
public void way2()
{
IFormatter formatter = new SoapFormatter();
Console.WriteLine("對象序列化開始....");
var me = new Person
{
Sno = "2008",
Name = "yuanyuan",
Sex = "man",
Age = 12

};

//都要加入[Serializable]中...
//建立一個檔案流
Stream s = new FileStream("",FileMode.OpenOrCreate,FileAccess.Write,FileShare.None);
formatter.Serialize(s,me);
s.Close();
Console.WriteLine("序列化結束.....\n");
Console.WriteLine("還原序列化....");


//還原序列化...
Stream st = new FileStream("",FileMode.Open,FileAccess.Read,FileShare.Read);
var stillme = (Person)formatter.Deserialize(st);
//這就就實現了我們的還原序列化的東西的呀...
st.Close();
Console.WriteLine("good job..well done");


//序列話之後,序列化之後,soap 格式的檔案(simple object access protocol)
//是一種輕量的,簡單的,基於xml的協議;它被設計在web上交換結構化的和固定化的資訊;
//soap 可以和現存的許多網際網路協議和格式結合使用...
//包括httpt smtp
//它還支援訊息系統到遠程調用(RPC)等大量的應用程式....
//它還支援從訊息系統到遠端程序呼叫(RPC)等大量的應用程式。


}


//方式三:使用xml 來進行序列化...
public void way3()
{
XmlSerializer formatter = new XmlSerializer(typeof(Person));
//對象的執行個體化開始...

var me = new Person()
{
Sno="2008123",
Name="yuanyuan",
Sex="man",
Age=123
};

//還是要建立一個檔案流...
Stream s = new FileStream("",FileMode.OpenOrCreate,FileAccess.Write,FileShare.None);
formatter.Serialize(s,me);
s.Close();
Console.WriteLine("序列化結束...");
Console.WriteLine("開始還原序列化...");
//為毛要用jsp呢....
Stream de = new FileStream("",FileMode.Open,FileAccess.Read,FileShare.Read);
var xx = (Person)formatter.Deserialize(de);
de.Close();
//這樣就實現了我麼序列化的解析的啦....
//解析後就是一半點鐘額xml格式
//注意一點:xml序列化的方式只能儲存public的欄位和可選讀的欄位;
//對於private等類型的欄位不能進行序列化的哦...


}

}
}

c#序列化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.