Specify the XmlRoot and XmlAttribute for XML serialization : Xml serialization : XML C# Examples


C# Examples » XML » Xml serialization »

 

Specify the XmlRoot and XmlAttribute for XML serialization









    
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();
      }
        
}
    
   
  
   



Output

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


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo XML
» Xml serialization