Monday, November 12, 2007

.NET Remoting (1) Serialization




using System;

using System.Collections.Generic;

using System.Text;

namespace SerializationDemo

{

[Serializable]

public class SumOf

{

public SumOf()

{

}

public DecimalList Members = new DecimalList();

public decimal Sum,Avg;

public void Calculate()

{

this.Sum = 0;

foreach (decimal m in Members)

{

Sum += m;

}

this.Avg = Sum / Members.Count;

}

}

[Serializable]

public class DecimalList : List<decimal>

{

}

}




using System;

using System.Collections.Generic;

using System.Windows.Forms;

namespace SerializationDemo

{

static class Program

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.Run(new Form1());

}

}

}




using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Runtime.Serialization.Formatters.Binary;

using System.Runtime.Serialization.Formatters.Soap;

using System.Xml.Serialization;

namespace SerializationDemo

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

SumOf sObj;

private void button1_Click(object sender, EventArgs e)

{

// create a file stream to write the file

FileStream fileStream = new FileStream("DoSum.bin", FileMode.Create);

// use the CLR binary formatter

BinaryFormatter binaryFormatter = new BinaryFormatter();

// serialize to disk

binaryFormatter.Serialize(fileStream, sObj);

fileStream.Close();

}

private SumOf BuildSumObj()

{

SumOf sObj = new SumOf();

for (int i = 0; i < this.numericUpDown1.Value; i++)

{

sObj.Members.Add(i);

}

sObj.Calculate();

return sObj;

}

private void button2_Click(object sender, EventArgs e)

{

// create a file stream to write the file

FileStream fileStream = new FileStream("DoSum_Soap.xml", FileMode.Create);

// use the CLR binary formatter

SoapFormatter formatter = new SoapFormatter();

// serialize to disk

formatter.Serialize(fileStream, sObj);

fileStream.Close();

}

private void button3_Click(object sender, EventArgs e)

{

sObj = this.BuildSumObj();

this.toolStripStatusLabel1.Text =

string.Format("count:{0}, sum:{1}, avg:{2}", sObj.Members.Count, sObj.Sum, sObj.Avg);

}

private void button4_Click(object sender, EventArgs e)

{

// create a file stream to write the file

FileStream fileStream = new FileStream("DoSum.xml", FileMode.Create);

// use the CLR binary formatter

System.Xml.Serialization.XmlSerializer

formatter = new XmlSerializer(typeof(SumOf));

// serialize to disk

formatter.Serialize(fileStream, sObj);

fileStream.Close();

}

}

}

In this code, click to create object first, then you can serialize the object into binary, xml and Soap format.



<?xml version="1.0"?>

<SumOf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<Members>

<decimal>0</decimal>

<decimal>1</decimal>

<decimal>2</decimal>

<decimal>3</decimal>

<decimal>4</decimal>

<decimal>5</decimal>

<decimal>6</decimal>

<decimal>7</decimal>

<decimal>8</decimal>

<decimal>9</decimal>

</Members>

<Sum>45</Sum>

<Avg>4.5</Avg>

</SumOf>



<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<SOAP-ENV:Body>

<a1:SumOf id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SerializationDemo/SerializationDemo%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">

<Members href="#ref-3"/>

<Sum>45</Sum>

<Avg>4.5</Avg>

</a1:SumOf>

<a1:DecimalList id="ref-3" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SerializationDemo/SerializationDemo%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">

<List_x0060_1_x002B__items href="#ref-4"/>

<List_x0060_1_x002B__size>10</List_x0060_1_x002B__size>

<List_x0060_1_x002B__version>10</List_x0060_1_x002B__version>

</a1:DecimalList>

<SOAP-ENC:Array id="ref-4" SOAP-ENC:arrayType="xsd:decimal[16]">

<item>0</item>

<item>1</item>

<item>2</item>

<item>3</item>

<item>4</item>

<item>5</item>

<item>6</item>

<item>7</item>

<item>8</item>

<item>9</item>

<item>0</item>

<item>0</item>

<item>0</item>

<item>0</item>

<item>0</item>

<item>0</item>

</SOAP-ENC:Array>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

blog comments powered by Disqus