Csharp/C Sharp by API/System.Runtime.Serialization.Formatters.Soap/SoapFormatter

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

new SoapFormatter()

<source lang="csharp">

using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Soap; [Serializable] class Customer {

 private int CustomerNumber;
 private string CustomerName;
 private string CustomerCountry;
 public void WriteCustomer()
 {
   Console.WriteLine("Customer Number: " + this.CustomerNumber);
 }
 public Customer(int newCustomerNumber, string newCustomerName, string newCustomerCountry)
 {
   this.CustomerNumber = newCustomerNumber;
   this.CustomerName = newCustomerName;
   this.CustomerCountry = newCustomerCountry;
 }

} public class MainClass {

 public static void Main() 
 {
   Customer MyCustomer = new Customer(1, "X Corporation", "France");
   MyCustomer.WriteCustomer();
   FileStream serializeStream = new FileStream("c:\\MyCustomer.xml", FileMode.Create);
   SoapFormatter sf = new SoapFormatter();
   sf.Serialize(serializeStream, MyCustomer);
   serializeStream.Flush();
   serializeStream.Close();
   FileStream retrieveStream = new FileStream("c:\\MyCustomer.xml",FileMode.Open);
   Customer NewCustomer = (Customer) sf.Deserialize(retrieveStream);
   NewCustomer.WriteCustomer();
 }

}


 </source>


SoapFormatter.Serialize

<source lang="csharp"> using System; using System.IO; using System.Collections; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Soap; public class FrameworksOverviewSerialization {

   public static void Main()
   {
       MyRow row = new MyRow();
       row.Add(new MyElement("Gumby"));
       row.Add(new MyElement("Pokey"));
       
       Console.WriteLine("Initial value");
       Console.WriteLine("{0}", row);
       
       Stream streamWrite = File.Create("MyRow.bin");
       BinaryFormatter binaryWrite = new BinaryFormatter();
       binaryWrite.Serialize(streamWrite, row);
       streamWrite.Close();
       
       Stream streamRead = File.OpenRead("MyRow.bin");
       BinaryFormatter binaryRead = new BinaryFormatter();
       MyRow rowBinary = (MyRow) binaryRead.Deserialize(streamRead);
       streamRead.Close();
       
       Console.WriteLine("Values after binary serialization");
       Console.WriteLine("{0}", rowBinary);
       
       streamWrite = File.Create("MyRow.xml");
       SoapFormatter soapWrite = new SoapFormatter();
       soapWrite.Serialize(streamWrite, row);
       streamWrite.Close();
       
       streamRead = File.OpenRead("MyRow.xml");
       SoapFormatter soapRead = new SoapFormatter();
       MyRow rowSoap = (MyRow) soapRead.Deserialize(streamRead);
       streamRead.Close();
       
       Console.WriteLine("Values after SOAP serialization");
       Console.WriteLine("{0}", rowSoap);
   }

} [Serializable] public class MyElement {

   public MyElement(string name)
   {
       this.name = name;
       this.cacheValue = 15;
   }
   public override string ToString()
   {
       return(String.Format("{0}: {1}", name, cacheValue));
   }
   string name;
   // this field isn"t persisted.
   [NonSerialized]
   int cacheValue;

} [Serializable] public class MyRow {

   public void Add(MyElement my)
   {
       row.Add(my);
   }
   
   public override string ToString()
   {
       string temp = null;
       foreach (MyElement my in row)
       temp += my.ToString() + "\n"; 
       return(temp);
   }
   
   ArrayList row = new ArrayList();    

}

 </source>