C#中怎么利用XML實現(xiàn)序列化

這篇文章給大家介紹C#中怎么利用XML實現(xiàn)序列化,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創(chuàng)新互聯(lián)建站是一家集成都做網(wǎng)站、成都網(wǎng)站設(shè)計、網(wǎng)站頁面設(shè)計、網(wǎng)站優(yōu)化SEO優(yōu)化為一體的專業(yè)的建站公司,已為成都等多地近百家企業(yè)提供網(wǎng)站建設(shè)服務(wù)。追求良好的瀏覽體驗,以探求精品塑造與理念升華,設(shè)計最適合用戶的網(wǎng)站頁面。 合作只是第一步,服務(wù)才是根本,我們始終堅持講誠信,負(fù)責(zé)任的原則,為您進行細(xì)心、貼心、認(rèn)真的服務(wù),與眾多客戶在蓬勃發(fā)展的市場環(huán)境中,互促共生。

序列化是將一個對象轉(zhuǎn)換成字節(jié)流以達(dá)到將其長期保存在內(nèi)存、數(shù)據(jù)庫或文件中的處理過程。它的主要目的是保存對象的狀態(tài)以便以后需要的時候使用。與其相反的過程叫做反序列化。

序列化一個對象
為了序列化一個對象,我們需要一個被序列化的對象,一個容納被序列化了的對象的(字節(jié))流和一個格式化器。進行序列化之前我們先看看System.Runtime.Serialization名字空間。ISerializable接口允許我們使任何類成為可序列化的類。

如果我們給自己寫的類標(biāo)識[Serializable]特性,我們就能將這些類序列化。除非類的成員標(biāo)記了[NonSerializable],序列化會將類中的所有成員都序列化。

序列化的類型

二進制(流)序列化
SOAP序列化(使用IXmlSerializable)
XML序列化
通常大部分都是使用的XML序列化,所以介紹一下使用XML序列化.

Student類

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

namespace XmlSerializers
{
[Serializable]
public   class Student
{
private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private int age;

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    private string hoddy;

    public string Hoddy
    {
        get { return hoddy; }
        set { hoddy = value; }
    }

    public Student()
    {
    }

    public Student(string name,int age,string hoddy)
    {
        this.Name = name;
        this.Age = age;
        this.Hoddy = hoddy;
    }

    public void SayHi()
    {
        string mess = string.Format("我是{0},年齡有{1},愛好是{2}", Name, Age, Hoddy);
        Console.WriteLine(mess);
    }

}

}

Program

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

using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace XmlSerializers
{
[Serializable]
class Program
{

    static  List<Student> students = new List<Student>();

 static  void Main(string[] args)
    {

       //添加學(xué)生
        InitStudent();
     //序列化
        Serialize();
    //反序列化
        Deserialize();

        Console.ReadLine();
    }

 public static void InitStudent()
 {

     Student scoficedld = new Student("scofield", 28, "哈哈");
     Student su = new Student("程沐喆", 34, "寫博客");
     Student zhang = new Student("張婧", 21, "唱歌");
     Student huang = new Student("黃飛鴻", 25, "打架");
     Student ding = new Student("丁俊暉", 30, "打斯諾克");
     Student sullivan = new Student("OSullivan", 33, "打147");
     Student Jay = new Student("周杰", 21, "耍雙節(jié)棍");

     students.Add(scoficedld);
     students.Add(su);
     students.Add(zhang);
     students.Add(huang);
     students.Add(ding);
     students.Add(sullivan);
     students.Add(Jay);
 }

 public static void Serialize()
 {
     FileStream fs = new FileStream("Serialize.xml", FileMode.Create);

     XmlSerializer xs = new XmlSerializer(typeof(List<Student>));

     xs.Serialize(fs, students);
     fs.Close();

 }

   public static  void Deserialize()
   {
       FileStream fs = new FileStream("Serialize.xml", FileMode.Open);
       XmlSerializer xs = new XmlSerializer(typeof(List<Student>));

     List<Student>  lists = xs.Deserialize(fs) as List<Student>;

       if (lists !=  null)
       {
           for (int i = 0; i < lists.Count; i++)
           {
               lists[i].SayHi();
           }
       }
       fs.Close();
   }

}

關(guān)于C#中怎么利用XML實現(xiàn)序列化就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

文章名稱:C#中怎么利用XML實現(xiàn)序列化
網(wǎng)頁路徑:http://muchs.cn/article46/jcpoeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)網(wǎng)站營銷、網(wǎng)站改版、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作