Serialize Class to Soap message in CSharp

Description

The following code shows how to serialize Class to Soap message.

Example


using System;//from w  ww  .  ja v  a  2  s .com
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;

public class Serializer {

  public static void Main(string [] args) {
    StudentList personnel = CreateStudentList();
    IFormatter soapFormatter = new SoapFormatter();
    using (FileStream stream = File.OpenWrite("StudentListSoap.xml")) {
      soapFormatter.Serialize(stream,personnel);
    }
  }
  
  private static StudentList CreateStudentList() {
    StudentList personnel = new StudentList();
    personnel.Students = new Employee [] {new Employee()};
    personnel.Students[0].FirstName = "Apple";
    personnel.Students[0].MiddleInitial = "M";
    personnel.Students[0].LastName = "Bear";
    
    personnel.Students[0].Addresses = new Address [] {new Address()};
    personnel.Students[0].Addresses[0].AddressType = AddressType.Home;
    personnel.Students[0].Addresses[0].Street = new string [] {"Culloden"};
    personnel.Students[0].Addresses[0].City = "Vancouver";
    personnel.Students[0].Addresses[0].State = State.BC;
    personnel.Students[0].Addresses[0].Zip = "V5V 4X7";
    
    personnel.Students[0].StartDate = new DateTime(2006,10,12);
    
    return personnel;
  }
}

[Serializable]
public enum AddressType {
  Home,
  Office
}

[Serializable]
public enum State {
  BC, ON
}

[Serializable]
public class Address {
  public AddressType AddressType;
  public string[] Street;
  public string City;
  public State State;
  public string Zip;
}

[Serializable]
public class TelephoneNumber {
  public string AreaCode;
  public string Exchange;
  public string Number;
}

[Serializable]
public class Employee {
  public string FirstName;
  public string MiddleInitial;
  public string LastName;
  public Address [] Addresses;
  public TelephoneNumber [] TelephoneNumbers;
  public DateTime StartDate;
}

[Serializable]
public class StudentList {
  public Employee [] Students;
}




















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