对象的本地保存

对象的创建和保存

对象的特点:

  • 对象“生活”在内存空间中,因此,程序一旦关闭,这些对象也都会被CLR的垃圾回收机制销毁。
  • 程序第二次运行时,对象会以“全新”的状态出现,无法保留上次对象的运行状态。
  • 如果希望第二次运行程序时能“重现”第一次运行时对象的“状态”, 则应用程序就必须采用某种方式将对象的各个属性的值保存到磁盘文件中, 这样在需要时可以从磁盘文件中重新设置对象的各个属性值,典型的方法就是使用文本文件保存对象的各个属性值。

要实现的功能:

  • 将用户信息封装为对象的属性并保存在文本中。
  • 将文本的信息还原成对象的属性并显示出来。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 对象的创建和保存
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            People people = new People()
            {
                Name = textBox1.Text,
                Age = int.Parse(textBox2.Text),
                Sex = textBox3.Text,
                Birth = DateTime.Parse(textBox4.Text),
            };
            FileStream fs = new FileStream("People.obj", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(people.Name);
            sw.WriteLine(people.Age);
            sw.WriteLine(people.Sex);
            sw.WriteLine(people.Birth);
            sw.Close();
            fs.Close();
            MessageBox.Show("数据保存成功");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("People.obj", FileMode.Open);
            StreamReader  sr=new StreamReader(fs);

            People people = new People()
            {
                Name = sr.ReadLine(),
                Age = int.Parse(sr.ReadLine()),
                Sex = sr.ReadLine(),
                Birth = DateTime.Parse(sr.ReadLine()),
            };


            textBox1.Text = people.Name;
            textBox2.Text = people.Age.ToString();




        }
    }
}

缺点:针对上面的存储和读取,顺序是比较重要的,一旦下面的读取顺序错乱(就是存储的顺序和读取的顺序不一致),就会导致数据错乱。

如果 People.obj 文件是别人给的,并且里面存储的属性比较多,那么读取起来就比较麻烦。

什么是序列化和反序列化

序列化:序列化是将对象状态转换为可保存或传输的格式的过程,比如转化为二进制、xml、json等的过程。

反序列化:与序列化相对的是反序列化,它将流转换为对象,也就是将在序列化过程中所生成的二进制串、xml、json等转换成数据结构或者对象的过程

序列化的三种方式

二进制序列化

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _01_二进制序列化
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ToBinaryFormatter();
        }

        public void ToBinaryFormatter()
        {
            //序列化

            People p1 = new People()
            {
                Name = "张三",
                Age = 20,
                Sex = "男"
            };


            //1.创建文件流
            //FileStream fs = new FileStream("1.txt", FileMode.Create);
            2.创建一个二进制序列化格式器
            //BinaryFormatter binaryFormatter = new BinaryFormatter();
            Serialize() 序列化p1对象 并存储到fs文件流中
            //binaryFormatter.Serialize(fs, p1);
            //fs.Close();


            //反序列化

            FileStream fs = new FileStream("1.txt", FileMode.Open);

            BinaryFormatter binaryFormatter = new BinaryFormatter();

            People p2=  (People)binaryFormatter.Deserialize(fs);
            fs.Close();
            Console.WriteLine(p2.Name);
            Console.WriteLine(p2.Age);

        }
    }

    //特性
    //作用:可以序列化的标识
    [Serializable]
    class People
    {
        public string Name { get; set; }
        public int Age {  get; set; }
        public string Sex {  get; set; }
    }
}

JSON序列化

JSON 全称“JavaScript Object Notation”,译为“JavaScript 对象简谱”或“JavaScript 对象表示法”,是一种轻量级的、基于文本的、开放的数据交换格式。

数据交换是指,两个设备之间建立连接并互相传递数据的过程。

[] 代表数组,{} 代表对象 Name Age代表属性

[ {“Name”:“1”,“Age”:“1”,“NickName”:“1”},{“Name”:“1”,“Age”:“1”,“NickName”:“1”}]

原生方式

  public void ToJson1()
  {

      People p1 = new People()
      {
          Name = "张三",
          Age = 20,
          Sex = "男"
      };


      json序列化
      //FileStream fs = new FileStream("1.json", FileMode.Create);

      //DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(People));
      //jsonSerializer.WriteObject(fs, p1);
      //fs.Close();

      //json 反序列化
      FileStream fs = new FileStream("1.json", FileMode.Open);
      DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(People));

       People p2=  jsonSerializer.ReadObject(fs) as People;
      Console.WriteLine(p2.Name);
      MessageBox.Show(p2.Name);

  }

第三方JsonConvert

1.项目右键–>管理NuGet程序包–>浏览–>搜索Newtonsoft.Json点击下载安装 —>项目引入Newtonsoft.Json;

 public void ToJson2()
 {
     People p1 = new People()
     {
         Name = "张三",
         Age = 20,
         Sex = "男"
     };
     //序列化

     //string JsonStr=  JsonConvert.SerializeObject(p1);
     //FileStream fs = new FileStream("2.json", FileMode.Create);

     //StreamWriter sr=new StreamWriter(fs);
     //sr.Write(JsonStr);  
     //sr.Close();
     //fs.Close();


     FileStream fs = new FileStream("2.json", FileMode.Open);


     StreamReader sr = new StreamReader(fs);
     string JsonStr= sr.ReadToEnd();
     sr.Close();
     fs.Close();

      MessageBox.Show(JsonStr);

     People p2=  JsonConvert.DeserializeObject<People>(JsonStr);
     MessageBox.Show(p2.Name);
 }

XML序列化

在网络传输过程中,XML 比较重要,也是一种数据传输格式。在各式各样的程序配置文件中,也经常用 XML 作为配置文件的写法。在 C# 中 XML 也扮演着重要的角色。

什么是 XML

  • XML 是 eXtensible Markup Language 的缩写, 即可扩展标记语言。
  • 它是一种可以用来创建自定义的标记语言,由万维网协会(W3C)创建,用来克服HTML的局限。
  • 从使用功能上看, XML 主要用于数据的存储,而 HTML 主要用于数据显示。

XML 文档的格式要求

  • 确定且唯一的根元素
  • 开始标签和结束标签匹配
  • 元素标签的正确嵌套
  • 属性值要用引号括起来
  • 同一个元素的属性不能重复

XML 语法要求

  • 元素: <标签>文本内容</标签>
  • 处理指令: <?xml version= "1.0"?>
  • 注释: <!--这是一个XML注释-->
  • 属性:<salary currency="US$"> 25000 </salary>

XML 应用示例演示

跨平台数据交互,典型应用就是webservice的使用

 People p1 = new People()
 {
     Name = "张三",
     Age = 20,
     Sex = "男"
 };

 //FileStream fs = new FileStream("1.xml", FileMode.Create);
 //StreamWriter sw= new StreamWriter(fs);

 xml格式器
 //XmlSerializer serializer = new XmlSerializer(typeof(People));


 //serializer.Serialize(sw, p1);
 //sw.Close();
 //fs.Close();




 FileStream fs = new FileStream("1.xml", FileMode.Open);

 StreamReader sr = new StreamReader(fs);

 XmlSerializer serializer = new XmlSerializer(typeof(People));
 People p2= serializer.Deserialize(sr) as People;
 MessageBox.Show(p2.Name);

XML 文件的生成

生成 XML 文件

生成 XMLFile1.xml 文件




<?xml version="1.0" encoding="utf-8" ?>
<Students>
	<Student>
		<StuName>高启强</StuName>
		<StuAge>48</StuAge>
		<StuGender>男</StuGender>
		<StuClass>C#一班</StuClass>
	</Student>
	<Student>
		<StuName>孟钰</StuName>
		<StuAge>16</StuAge>
		<StuGender>女</StuGender>
		<StuClass>C#一班</StuClass>
	</Student>
	<Student>
		<StuName>小五</StuName>
		<StuAge>22</StuAge>
		<StuGender>女</StuGender>
		<StuClass>C#二班</StuClass>
	</Student>
	<Student>
		<StuName>安欣</StuName>
		<StuAge>21</StuAge>
		<StuGender>男</StuGender>
		<StuClass>C#三班</StuClass>
	</Student>
	<Student>
		<StuName>赵立冬</StuName>
		<StuAge>23</StuAge>
		<StuGender>男</StuGender>
		<StuClass>C#三班</StuClass>
	</Student>
	<DataInfo>
        <Version versionNum="2.1" pTime="2023-03-28">数据版本信息</Version>
    </DataInfo>
</Students>

然后把编辑好的 XMLFile1.xml 文件,放到 Debug 文件夹中,等待读取。

读取 XML 文件

//节点==元素==标签

            //xml文件读取的基本操作
            //1.创建XML文档操作对象
            XmlDocument xmlDoc = new XmlDocument();
            //2.加载xml文件到文档对象中
            xmlDoc.Load(@"XMLFile1.xml");
            //3.获取xml文档的跟目录
            XmlNode rootNode=  xmlDoc.DocumentElement;

            //存储所有的学生信息
            List<Student> students=new List<Student>();

            //4.遍历跟节点,获取根节点中所有的节点
            foreach (XmlNode stuNode in rootNode.ChildNodes)
            {

                if (stuNode.Name== "Student")
                {
                    Student stu =new Student();
                    foreach (XmlNode subNode in stuNode.ChildNodes)
                    {

                        //根据子节点的名称封装到对象的属性中
                        switch (subNode.Name)
                        {
                            case"StuName":
                                //InnerText 获取节点中的文本内容
                                stu.StuName = subNode.InnerText;
                                break;
                            case "StuAge":
                                //InnerText 获取节点中的文本内容
                                stu.StuAge = int.Parse(subNode.InnerText);
                                break;

                            case "StuGender":
                                //InnerText 获取节点中的文本内容
                                stu.StuGender = subNode.InnerText;
                                break;

                            case "StuClass":
                                //InnerText 获取节点中的文本内容
                                stu.StuClass = subNode.InnerText;
                                break;
                            default:
                                break;
                        }

                    }
                    students.Add(stu);
                }

            }

         dataGridView1.DataSource= students;

XML 文件读取总结

常用对象:

  • XmlDocument 对象表示 XML 整个文档
  • XmlNode 对象表示 XML 文件的单个节点

常用属性与说明:

对象 属性和方法 说明
XmlDocument DocumentElement属性 获取根节点
ChildNodes属性 获取所有子节点
Load()方法 读取整个XML的结构
XmlNode InnerText属性 当前节点的值
Name属性 当前节点的名字
ChildNodes属性 当前节点的所有子节点

json 和 xml的区别 : 都是数据格式

1.xml属于重量级别 json是属于轻量级别

2.xml在传输的过程中比较占宽带, json占宽带少

3.xml和json 解析方式不一样,xml使用 XMLDocument类 ,Json解析方式可以使用内置的类和第三方类库

json 和 xml的区别 : 都是数据格式

1.xml属于重量级别 json是属于轻量级别

2.xml在传输的过程中比较占宽带, json占宽带少

3.xml和json 解析方式不一样,xml使用 XMLDocument类 ,Json解析方式可以使用内置的类和第三方类库

Logo

全面兼容主流 AI 模型,支持本地及云端双模式

更多推荐