Set Customized Serialization Rule in CSharp

Description

The following code shows how to set Customized Serialization Rule.

Example


using System.Runtime.Serialization.Formatters.Soap;
using System.IO;//  w  w  w  .  j a va 2s  . c o m
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

  [Serializable]
  class StringData : ISerializable
  {
    public string dataItemOne = "First";
    public string dataItemTwo= "data";

    public StringData() { }
    protected StringData(SerializationInfo si, StreamingContext ctx)
    {
      dataItemOne = si.GetString("First").ToLower();
      dataItemTwo = si.GetString("data").ToLower();
    }

    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext ctx)
    {
      info.AddValue("First", dataItemOne.ToUpper());
      info.AddValue("data", dataItemTwo.ToUpper());
    }
  }
  [Serializable]
  class MoreData
  {
    public string dataItemOne = "block";
    public string dataItemTwo = "data";

    [OnSerializing]
    private void OnSerializing(StreamingContext context)
    {
      dataItemOne = dataItemOne.ToUpper();
      dataItemTwo = dataItemTwo.ToUpper();
    }

    [OnDeserialized]
    private void OnDeserialized(StreamingContext context)
    {
      dataItemOne = dataItemOne.ToLower();
      dataItemTwo = dataItemTwo.ToLower();
    }
  }
  class Program
  {
    static void Main(string[] args)
    {
      MoreData myData = new MoreData();
      SoapFormatter soapFormat = new SoapFormatter();
      using (Stream fStream = new FileStream("MyData.soap",FileMode.Create, FileAccess.Write, FileShare.None))
      {
        soapFormat.Serialize(fStream, myData);
      }
    }
  }




















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip