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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
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
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.