11.6.10

3 arten der Serialisierung bzw Deserialisierung in C#

Man versteht darunter eine Abbildung von Objekten auf eine externe sequenzielle Darstellungsform. Serialisierung kann für das Erreichen von Persistenz für ein Objekt verwendet werden, aber auch in verteilten Softwaresystemen spielt Serialisierung eine bedeutende Rolle.[Quelle Wikipedia: http://de.wikipedia.org/wiki/Serialisierung]

Es gibt unter C# 3 Arten der Serialisierung:
* BinaryFormatter (Objekte werden binär abgespeichert)
* SoapFormatter (Objekte können gelesen werden)
* XMLSerializer (Objekte werden in eine XML Datei gespeichert)

using System;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;
using System.IO;
using System.Collections;


namespace SerializationSample
{
public class Tester
{

private readonly string serFolder;
private static ArrayList arrList = new ArrayList();

public Tester()
{
serFolder = Path.GetTempPath();
}

public static void Main()
{
Tester abc = new Tester();
arrList.Add((Person) abc.AddPerson("Hans", "Müller", "CH", 39));
arrList.Add((Person) abc.AddPerson("Luft", "Hansa", "D", 82));
arrList.Add((Person) abc.AddPerson("Soto", "Mayer", "ZSQ", 55));

abc.serializeXml();
abc.serializeSoap();
abc.serializeBinary();
Console.WriteLine("Press enter to begin deserialization...");
Console.ReadLine();
abc.deserializeXml();
abc.deserializeSoap();
abc.deserializeBinary();
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}

//....(ArrayList füllen .....)
private object AddPerson(string Vorname, string Name, string Land, int Alter)
{
Person p = new Person();
p.Vorname = Vorname;
p.Name = Name;
p.Land = Land;
p.Alter = Alter;
return p;
}

/*****************
* XmlSerializer *
*****************/
private void serializeXml() {
Console.WriteLine("Serializing in XML...\n");
using (FileStream fs = new FileStream(Path.Combine(serFolder, "Personen.xml"), FileMode.Create)) {
XmlSerializer xmlSer = new XmlSerializer(typeof(ArrayList), new Type[] { typeof(Person) });
xmlSer.Serialize(fs, arrList);
}
}

private void deserializeXml() {
try {
using (FileStream fs = new FileStream(Path.Combine(serFolder, "Personen.xml"), FileMode.Open)) {
XmlSerializer xmlSer = new XmlSerializer(typeof(ArrayList), new Type[] { typeof(Person) });
arrList = (ArrayList) xmlSer.Deserialize(fs);
}
Console.WriteLine("Deserializing XML...\n");
foreach (Person p in arrList)
Console.WriteLine(p.ToString());
}
catch (IOException ex) {
Console.WriteLine(ex.Message);
}
}

/******************
* Soap Formatter *
******************/
private void serializeSoap() {
Console.WriteLine("Serializing in SOAP...\n");
using (FileStream fs = new FileStream(Path.Combine(serFolder, "soap.dat"), FileMode.Create)) {
SoapFormatter soaFormatter = new SoapFormatter();
soaFormatter.Serialize(fs, arrList);
}
}

private void deserializeSoap() {
try {
using (FileStream fs = new FileStream(Path.Combine(serFolder, "soap.dat"), FileMode.Open)) {
SoapFormatter soaFormatter = new SoapFormatter();
arrList = (ArrayList)soaFormatter.Deserialize(fs);
}
Console.WriteLine("Deserializing SOAP...\n");
foreach (Person p in arrList)
Console.WriteLine(p.ToString());
}
catch (IOException ex) {
Console.WriteLine(ex.Message);
}
}

/********************
* Binary Formatter *
********************/
private void serializeBinary() {
Console.WriteLine("Serializing in Binary...\n");
using (FileStream fs = new FileStream(Path.Combine(serFolder, "binary.dat"), FileMode.Create)) {
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(fs, arrList);
}
}

private void deserializeBinary() {
try {
using (FileStream fs = new FileStream(Path.Combine(serFolder, "binary.dat"), FileMode.Open)) {
BinaryFormatter binFormatter = new BinaryFormatter();
arrList = (ArrayList)binFormatter.Deserialize(fs);
}
Console.WriteLine("Deserializing Binary...\n");
foreach (Person p in arrList)
Console.WriteLine(p.ToString());
}
catch (IOException ex) {
Console.WriteLine(ex.Message);
}
}
}

//Die zu serialisierende Klasse
//(Die XML-Attribute werden nur für die Xml-serialisierung gebraucht)
[Serializable()]
public class Person {
[XmlElement("Vorname",DataType="string")]
private string vorname;
[XmlElement("Nachname", DataType = "string")]
private string name;
[XmlElement("Alter", DataType = "int")]
private int alter;
[XmlAttribute("Land", DataType = "string")]
private string land;

public Person() { }

public string Vorname {
get { return vorname; }
set { vorname = value; }
}

public string Name {
get { return name; }
set { name = value; }
}

public int Alter {
get { return alter; }
set { alter = value; }
}

public string Land {
get { return land; }
set { land = value; }
}

public override String ToString() {
return vorname + "\t" + name + "\t" + alter + "\t" + land;
}
}
}





Nvidia's GauGan App

NVIDIA's GauGAN AI Machine Learning Tool creates photorealistic images from Simple Hand Doodling http://nvidia-research-mingyuliu.com/...