⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 C#开发教程 由浅入深 配有实例 是初学者的好帮手
💻 CS
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -