Use XML Serialization with Custom Objects : Serialization « File Stream « 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 » File Stream » SerializationScreenshots 
Use XML Serialization with Custom Objects
Use XML Serialization with Custom Objects
 

using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

public class SerializeXml {
    private static void Main() {
        CarList catalog = new CarList("New List", DateTime.Now.AddYears(1));
        Car[] cars = new Car[2];
        cars[0new Car("Car 1"12342.99m);
        cars[1new Car("Car 2"21234123.9m);
        catalog.Cars = cars;

        XmlSerializer serializer = new XmlSerializer(typeof(CarList));
        FileStream fs = new FileStream("CarList.xml", FileMode.Create);
        serializer.Serialize(fs, catalog);
        fs.Close();

        catalog = null;

        // Deserialize the order from the file.
        fs = new FileStream("CarList.xml", FileMode.Open);
        catalog = (CarList)serializer.Deserialize(fs);

        // Serialize the order to the Console window.
        serializer.Serialize(Console.Out, catalog);
    }
}


[XmlRoot("carList")]
public class CarList {

    [XmlElement("catalogName")]
    public string ListName;
    
    // Use the date data type (and ignore the time portion in the serialized XML).
    [XmlElement(ElementName="expiryDate", DataType="date")]
    public DateTime ExpiryDate;
    
    [XmlArray("cars")]
    [XmlArrayItem("car")]
    public Car[] Cars;

    public CarList() {
    }

    public CarList(string catalogName, DateTime expiryDate) {
        this.ListName = catalogName;
        this.ExpiryDate = expiryDate;
    }
}

public class Car {

    [XmlElement("carName")]
    public string CarName;
    
    [XmlElement("carPrice")]
    public decimal CarPrice;
    
    [XmlElement("inStock")]
    public bool InStock;
    
    [XmlAttributeAttribute(AttributeName="id", DataType="integer")]
    public string Id;

    public Car() {
    }
    public Car(string carName, decimal carPrice) {
        this.CarName = carName;
        this.CarPrice = carPrice;
    }
}
           
         
  
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.Three types of Serialization: Binary, Soap, XML
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
15.Serializes an object to binary
16.Deserializes an object from binary
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.