SOAP serialization in C#

By: Jason Price Emailed: 1608 times Printed: 2078 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This sample C# program illustrates SOAP serialization.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;

// the Customer class gives us something to serialize
[Serializable]
class Customer
{
  // some private data members
  private int CustomerNumber;
  private string CustomerName;
  private string CustomerCountry;

  // the WriteCustomer method formats info to the screen
  public void WriteCustomer()
  {
    Console.WriteLine("Customer Number: " + this.CustomerNumber);
    Console.WriteLine("Customer Name: " + this.CustomerName);
    Console.WriteLine("Customer Country: " + this.CustomerCountry);
  }

  // the constructor accepts all the info to create a customer
  public Customer(
    int newCustomerNumber, 
    string newCustomerName, 
    string newCustomerCountry)
  {
    this.CustomerNumber = newCustomerNumber;
    this.CustomerName = newCustomerName;
    this.CustomerCountry = newCustomerCountry;
  }
}

public class Example15_20 
{

  public static void Main() 
  {

    // create a new customer and dump to screen
    Customer MyCustomer = new Customer(1, "X Corporation", "France");
    MyCustomer.WriteCustomer();

    // Create a FileStream to hold the serialized customer
    FileStream serializeStream = new FileStream("c:\\MyCustomer.xml", 
      FileMode.Create);

    // use SOAP formatting
    SoapFormatter sf = new SoapFormatter();

    // serialize the object
    sf.Serialize(serializeStream, MyCustomer);
    serializeStream.Flush();
    serializeStream.Close();

    // retrieve the serialized version to a second object and dump that
    FileStream retrieveStream = new FileStream("c:\\MyCustomer.xml",
      FileMode.Open);
    Customer NewCustomer = (Customer) sf.Deserialize(retrieveStream);
    NewCustomer.WriteCustomer();
  }

}

Csharp Home | All Csharp Tutorials | Latest Csharp Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Jason Price
SOAP serialization in C#

More Tutorials in Csharp
Major features of C#
Categories of datatypes in C#
Boxing and unboxing in C#
Preprocessor directives in C#
Comments in C#
Hello World sample program in C#
SOAP serialization in C#
What is C#?
Design Goals of C#
Why it was named C#?
History of C#?
Which version of C# to use?
Convert Image to Base64 String in C#
Convert Base64 String to Image in C#

More Latest News
Most Viewed Articles (in Csharp )
Convert Image to Base64 String in C#
SOAP serialization in C#
What is C#?
Why it was named C#?
Convert Base64 String to Image in C#
Design Goals of C#
History of C#?
Which version of C# to use?
Major features of C#
Categories of datatypes in C#
Boxing and unboxing in C#
Preprocessor directives in C#
Comments in C#
Hello World sample program in C#
Most Emailed Articles (in Csharp)
History of C#?
Convert Image to Base64 String in C#
What is C#?
Convert Base64 String to Image in C#
Why it was named C#?
SOAP serialization in C#
Design Goals of C#
Comments in C#
Which version of C# to use?
Major features of C#
Preprocessor directives in C#
Hello World sample program in C#
Categories of datatypes in C#
Boxing and unboxing in C#