温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中怎么利用XML实现序列化

发布时间:2021-08-07 16:09:49 来源:亿速云 阅读:123 作者:Leah 栏目:编程语言

这篇文章给大家介绍C#中怎么利用XML实现序列化,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

序列化是将一个对象转换成字节流以达到将其长期保存在内存、数据库或文件中的处理过程。它的主要目的是保存对象的状态以便以后需要的时候使用。与其相反的过程叫做反序列化。

序列化一个对象
为了序列化一个对象,我们需要一个被序列化的对象,一个容纳被序列化了的对象的(字节)流和一个格式化器。进行序列化之前我们先看看System.Runtime.Serialization名字空间。ISerializable接口允许我们使任何类成为可序列化的类。

如果我们给自己写的类标识[Serializable]特性,我们就能将这些类序列化。除非类的成员标记了[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)
    {

       //添加学生
        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, "耍双节棍");

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

}

关于C#中怎么利用XML实现序列化就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

xml
AI