class1.cs
来自「C#开发教程 由浅入深 配有实例 是初学者的好帮手」· CS 代码 · 共 65 行
CS
65 行
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
namespace SerializeSOAP
{
[Serializable]
public class Insect
{
private string name;
[NonSerialized]
private int id; // this field isn't persisted.
public Insect(string name, int id)
{
this.name = name;
this.id = id;
}
public override string ToString()
{
return String.Format("{0}: {1}", name, id);
}
}
class SerializeSOAPApp
{
[STAThread]
public static void Main(string[] args)
{
// Serialize one Insect
Insect i = new Insect("Meadow Brown", 12);
Stream sw = File.Create("Insects.xml");
SoapFormatter bf = new SoapFormatter();
bf.Serialize(sw, i);
sw.Close();
// Serialize a collection of Insects
ArrayList box = new ArrayList();
box.Add(new Insect("Marsh Fritillary", 34));
box.Add(new Insect("Speckled Wood", 56));
box.Add(new Insect("Milkweed", 78));
sw = File.Open("Insects.xml", FileMode.Append);
bf.Serialize(sw, box);
sw.Close();
// Deserialize one Insect
Stream sr = File.OpenRead("Insects.xml");
Insect j = (Insect)bf.Deserialize(sr);
Console.WriteLine(j);
// Deserialize the collection
ArrayList bag = (ArrayList) bf.Deserialize(sr);
sr.Close();
foreach (Insect k in bag)
{
Console.WriteLine(k);
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?