Three types of Serialization: Binary, Soap, XML : Serialization « File Stream « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Class Interface
3. Collections Data Structure
4. Components
5. Data Types
6. Database ADO.net
7. Design Patterns
8. Development Class
9. Event
10. File Stream
11. Generics
12. GUI Windows Form
13. Language Basics
14. LINQ
15. Network
16. Office
17. Reflection
18. Regular Expressions
19. Security
20. Services Event
21. Thread
22. Web Services
23. Windows
24. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / C Sharp » File Stream » SerializationScreenshots 
Three types of Serialization: Binary, Soap, XML
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;
using System.Collections;

[Serializable]
public class Radio {
    public bool hasTweeters;
    public bool hasSubWoofers;
    public double[] stationPresets;

    [NonSerialized]
    public string radioID = "001";
}

[Serializable]
public class Car {
    public Radio theRadio = new Radio();
    public bool isHatchBack;
}

[Serializable, XmlRoot(Namespace = "http://www.yoursite.com")]
public class JamesBondCar : Car {
    public JamesBondCar(bool SkyWorthy, bool SeaWorthy) {
        canFly = SkyWorthy;
        canSubmerge = SeaWorthy;
    }
    public JamesBondCar() { }
    [XmlAttribute]
    public bool canFly;
    [XmlAttribute]
    public bool canSubmerge;
}

class Program {
    static void Main(string[] args) {
        JamesBondCar jbc = new JamesBondCar();
        jbc.canFly = true;
        jbc.canSubmerge = false;
        jbc.theRadio.stationPresets = new double[] { 89.3105.197.1 };
        jbc.theRadio.hasTweeters = true;

        BinaryFormatter binFormat = new BinaryFormatter();
        Stream fStream = new FileStream("CarData.dat",FileMode.Create, FileAccess.Write, FileShare.None);
        binFormat.Serialize(fStream, jbc);
        fStream.Close();
        fStream = File.OpenRead("CarData.dat");
        JamesBondCar carFromDisk =(JamesBondCar)binFormat.Deserialize(fStream);
        Console.WriteLine("Can this car fly? : {0}", carFromDisk.canFly);
        fStream.Close();
        SoapFormatter soapForamt = new SoapFormatter();
        fStream = new FileStream("CarData.soap",FileMode.Create, FileAccess.Write, FileShare.None);
        soapForamt.Serialize(fStream, jbc);
        fStream.Close();
        XmlSerializer xmlForamt = new XmlSerializer(typeof(JamesBondCar),new Type[] { typeof(Radio), typeof(Car) });
        fStream = new FileStream("CarData.xml",FileMode.Create, FileAccess.Write, FileShare.None);
        xmlForamt.Serialize(fStream, jbc);
        fStream.Close();

        ArrayList myCars = new ArrayList();
        myCars.Add(new JamesBondCar(true, true));
        myCars.Add(new JamesBondCar(true, false));
        myCars.Add(new JamesBondCar(false, true));
        myCars.Add(new JamesBondCar(false, false));

        fStream = new FileStream("CarCollection.xml",FileMode.Create, FileAccess.Write, FileShare.None);

        xmlForamt = new XmlSerializer(typeof(ArrayList),new Type[] { typeof(JamesBondCar), typeof(Car), typeof(Radio) });
        xmlForamt.Serialize(fStream, myCars);
        fStream.Close();
    }
}

 
Related examples in the same category
1. Use Serializable attribute to mark a generic class
2. Use Serializable attribute to mark a class
3. Deserialize Object
4. Serialize and DeSerialize
5. Use XML Serialization with Custom ObjectsUse XML Serialization with Custom Objects
6. Working with the Serializable Attribute
7. NonSerialized attributesNonSerialized attributes
8. Serialize hiearchy classesSerialize hiearchy classes
9. C# Serialization C# Serialization
10. Serial Employee class
11. Set XML tag name for Serialization
12. illustrates binary serializationillustrates binary serialization
13. Deserialize
14. Collection Serialization
w___w__w_._j_a__v__a_2___s__.___c_o_m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.