Specify the XmlRoot and XmlAttribute for XML serialization : Xml serialization « XML « C# / CSharp Tutorial






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


[XmlRoot("EmployeeList")]
public class EmployeeList {
    private ArrayList employeeList = new ArrayList();

    public EmployeeList( ) {
    }

    [XmlElement("employee")]
     public Employee[] Employees {
        get { 
            Employee[] employees = new Employee[ employeeList.Count ];
            employeeList.CopyTo( employees );
            return employees; 
        }
        set { 
            Employee[] employees = (Employee[])value;
            employeeList.Clear();
            foreach( Employee i in employees )
               employeeList.Add( i );
        }
    }

    public void AddEmployee( Employee employee ) {
        employeeList.Add( employee );
    }

    public Employee this[string firstName] {
        get {
            foreach( Employee i in employeeList )
                if( i.firstName == firstName )
                   return i;
            throw( new Exception("Employee not found") );
        }
    }

    public void DisplayEmployees( ) {
        foreach( Employee i in employeeList )
            Console.WriteLine( i );
    }

}

public class Employee {
     [XmlAttribute("firstName")]   public string  firstName;
     [XmlAttribute("lastName")]    public string  lastName;
     [XmlAttribute("salary")]      public double  salary;
     [XmlAttribute("number")]      public int     number;

    
     public Employee( ) {  }

     public Employee( string f, string l, double s, int n ) {
        firstName = f;
        lastName = l;
        salary = s;
        number = n;
     }

     public override string ToString( ) {
        object[] o = new object[] { firstName, lastName, salary, number };
        return string.Format("{0,-5} {1,-10} ${2,5:#,###.00} {3,3}", o);
     }
}
public class MainClass {
    public static void Main( ) {
        EmployeeList employList = new EmployeeList( );

        employList.AddEmployee( new Employee("A","p",  123.15,100) );
        employList.AddEmployee( new Employee("B","c",  712.50, 25) );
        employList.AddEmployee( new Employee("C","wt", 132.35, 10) );

        employList.DisplayEmployees( );
        Console.WriteLine("Serialization in progress");
        XmlSerializer s = new XmlSerializer( typeof( EmployeeList ) );
        TextWriter w = new StreamWriter("employList.xml");
        s.Serialize( w, employList );
        w.Close();
        Console.WriteLine("Serialization complete\n\n");

        Console.WriteLine("Deserialization in progress");
        EmployeeList employList2 = new EmployeeList( );
        TextReader r = new StreamReader( "employList.xml" );
        employList2 = (EmployeeList)s.Deserialize( r );
        r.Close( );
        Console.WriteLine("Deserialization complete");
        employList2.DisplayEmployees();
   }
    
}
A     p          $123.15 100
B     c          $712.50  25
C     wt         $132.35  10
Serialization in progress
Serialization complete


Deserialization in progress
Deserialization complete
A     p          $123.15 100
B     c          $712.50  25
C     wt         $132.35  10








30.30.Xml serialization
30.30.1.XML serialization with namespace setting
30.30.2.Serialize/Deserialize Xml: deal with element list
30.30.3.Specify the XmlRoot and XmlAttribute for XML serialization
30.30.4.Specify format and indentation for object XML serialization
30.30.5.Using XmlSerializer to Serialize a Linq object
30.30.6.XML Serialization Sample
30.30.7.Serialization of an object marked with XmlAttribute and XmlIgnore
30.30.8.Serialize a list of object to Xml with Linq
30.30.9.Xml Serialization for Enum
30.30.10.Xml Serialization for DateTime value
30.30.11.Assign XmlArrayAttribute to two arrays, and serializes a class instance that contains those arrays.
30.30.12.XmlTextAttribute with type string informs the XmlSerializer that strings should be serialized as XML text