Serialize List of Objects : Xml Serialization « XML « C# / C Sharp

Home
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.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » XML » Xml SerializationScreenshots 
Serialize List of Objects
 


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 = "002";
}

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

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

class Program {
    static void Main(string[] args) {
        MyCar jbc = new MyCar();
        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");
        MyCar carFromDisk =(MyCar)binFormat.Deserialize(fStream);
        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(MyCar),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 MyCar(true, true));
        myCars.Add(new MyCar(true, false));
        myCars.Add(new MyCar(false, true));
        myCars.Add(new MyCar(false, false));

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

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

           
         
  
Related examples in the same category
1.Use XmlSerializer
2.Set Xml Attribute when serilzation
3.Use XML Serialization with Custom Objects
4.XmlRootAttribute
5.Takes an XML file and exports the Object it holds
6.Converts an XML string to an object
7.Deserializes/Serializes an xml document back into an object
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.