using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; using Newtonsoft.Json; namespace ConsoleApplication1 { [Serializable] public class Ornek { public string Adi { get; set; } public void calis() { } } class Program { public static Ornek o = new Ornek() { Adi = "cem" }; private static void Main(string[] args) { json(); } public static void json() { string json = JsonConvert.SerializeObject(o); Console.WriteLine(json); } public static void binary() { BinaryFormatter formatter = new BinaryFormatter(); FileStream stream = File.Create("c:\\temp\\test.txt"); Console.WriteLine("Serializing vector"); formatter.Serialize(stream, o); stream.Close(); } public static void xml(){ XmlSerializer x = new XmlSerializer(o.GetType()); x.Serialize(Console.Out, o); } } } /* ÇIKTILARIMIZ: * XML <?xml version="1.0" encoding="ibm857"?> <Ornek xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://w ww.w3.org/2001/XMLSchema"> <Adi>cem</Adi> </Ornek> * * BINARY ÿÿÿÿ JConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null ConsoleApplication1.Ornek <Adi>k__BackingField cem * * JSON {"Adi":"cem"} */
Kodumuz şöyle olacak:
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Soap; using System.Xml.Serialization; namespace ConsoleApplication7 { [Serializable] public class Basit { public char c = 'a'; public byte b = (byte)'a'; private char privateAmaBinaryFormatterSerilestirir_SoapFormatterSerilestirmez = (char)65; [XmlElement(DataType = "string", ElementName="ADI")] // XSD tanımlamaları public string Adi = "isimsiz"; [NonSerialized] // BinaryFormatter private'a bakmaksızın serileştirdiği için [NonSerialized] ile işaretliyoruzki, serileştirmesin private byte NonSerialized_isaretli_BinaryFormatter_Serilestirmez = 1; } public class Example { public static void Main() { var b = BinaryFormatla_seri_deseri_lestir(); var s = SoapFormatla_seri_deseri_lestir(); var x = XML_seri_deseri_lestir(); /* * Binary Serialization * Serialization can be defined as the process of storing the state of an object to a storage medium. * During this process, * the public and private fields of the object * and the name of the class, * including the assembly containing the class, * are converted to a stream of bytes, which is then written to a data stream. * When the object is subsequently deserialized, an exact clone of the original object is created. * When implementing a serialization mechanism in an object-oriented environment, you have to make a number of tradeoffs between ease of use and flexibility. * The process can be automated to a large extent, provided you are given sufficient control over the process. */ /* * XML and SOAP Serialization * XML serialization converts (serializes) the public fields and properties of an object, * or the parameters and return values of methods, * into an XML stream that conforms to a specific XML Schema definition language (XSD) document. * XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format (in this case, XML) for storage or transport. */ } private static Basit XML_seri_deseri_lestir() { Type t = Type.GetType("ConsoleApplication7.Basit"); XmlSerializer ser = new XmlSerializer(t); FileStream fs = new FileStream("XmlSer.xml",FileMode.OpenOrCreate); ser.Serialize(fs,new Basit()); fs.Close(); Basit x = (Basit) ser.Deserialize(new FileStream("XmlSer.xml", FileMode.Open)); return x; } private static Basit SoapFormatla_seri_deseri_lestir() { SoapFormatter soapFmt = new SoapFormatter(); FileStream fs = new FileStream("basit.xml", FileMode.OpenOrCreate); soapFmt.Serialize(fs, new Basit()); fs.Close(); FileStream fs1 = new FileStream("basit.xml", FileMode.Open); Basit s = (Basit)soapFmt.Deserialize(fs1); return s; } private static Basit BinaryFormatla_seri_deseri_lestir() { BinaryFormatter binaryFmt = new BinaryFormatter(); FileStream fs = new FileStream("basit.bin", FileMode.OpenOrCreate); binaryFmt.Serialize(fs, new Basit()); fs.Close(); FileStream fs1 = new FileStream("basit.bin", FileMode.Open); Basit b = (Basit)binaryFmt.Deserialize(fs1); return b; } } }ve çıktılardan önce dönen değişkenler şöyle: Dosyalardaki çıktılar ise: BINARY Formatlı Çıktının HEX Görünümü: SOAP XML Çıktısı:
<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:Basit id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/ConsoleApplication7/ConsoleApplication7%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull"> <c>a</c> <b>97</b> <privateAmaBinaryFormatterSerilestirir_SoapFormatterSerilestirmez>A</privateAmaBinaryFormatterSerilestirir_SoapFormatterSerilestirmez> <Adi id="ref-3">isimsiz</Adi> </a1:Basit> </SOAP-ENV:Body> </SOAP-ENV:Envelope>XML Çıktısı:
<?xml version="1.0"?> <Basit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <c>97</c> <b>97</b> <ADI>isimsiz</ADI> </Basit>