Use XML Serialization with Custom Objects : Serialization « File Stream « C# / C Sharp






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[0] = new Car("Car 1", 12342.99m);
        cars[1] = new 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
17.Object to string serialization/Deserialization
18.Object to byte array serialization/Deserialization
19.Javascript Serializer
20.Serialize and Deserialize (2)
21.Binary Serializer
22.Deserialize the incomming data to the T datatype, this method used to deserialize the test cases data to the required entity
23.Serialization Utility
24.Serialization Utilities
25.Clone With Serialization