45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:将一个自定义的结构体写入到文件中去的方法

将一个自定义的结构体写入到文件中去的方法

2016-08-30 13:11:46 来源:www.45fan.com 【

将一个自定义的结构体写入到文件中去的方法


using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
//using System.Runtime.Serialization.Formatters.Binary;

public class Test {
public static void Main() {

//Creates a new TestSimpleObject object.
TestSimpleObject obj = new TestSimpleObject();

Console.WriteLine("Before serialization the object contains: ");
obj.Print();

//Opens a file and serializes the object into it in binary format.
Stream stream = File.Open("data.xml", FileMode.Create);
SoapFormatter formatter = new SoapFormatter();

//BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, obj);
stream.Close();

//Empties obj.
obj = null;

//Opens file "data.xml" and deserializes the object from it.
stream = File.Open("data.xml", FileMode.Open);
formatter = new SoapFormatter();

//formatter = new Binary

 

本文地址:http://www.45fan.com/a/question/69849.html
Tags: 一个 下面 结构
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部