Serialize Class to Soap message : Serialization SOAP « File Stream « C# / C Sharp






Serialize Class to Soap message

using System;
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;
}

           
       








Related examples in the same category

1.Serialize to SOAP based XML fileSerialize to SOAP based XML file
2.Use SoapFormatter