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

C# / CSharp Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statement
5. String
6. struct
7. Class
8. Operator Overload
9. delegate
10. Attribute
11. Data Structure
12. Assembly
13. Date Time
14. Development
15. File Directory Stream
16. Preprocessing Directives
17. Regular Expression
18. Generic
19. Reflection
20. Thread
21. I18N Internationalization
22. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » XML » Xml serialization 
26. 24. 3. 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 EmployeeemployeeList.Count ];
            employeeList.CopyToemployees );
            return employees; 
        }
        set 
            Employee[] employees = (Employee[])value;
            employeeList.Clear();
            foreachEmployee i in employees )
               employeeList.Add);
        }
    }

    public void AddEmployeeEmployee employee ) {
        employeeList.Addemployee );
    }

    public Employee this[string firstName] {
        get {
            foreachEmployee i in employeeList )
                ifi.firstName == firstName )
                   return i;
            thrownew Exception("Employee not found") );
        }
    }

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

}

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 Employeestring f, string l, double s, int ) {
        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.AddEmployeenew Employee("A","p",  123.15,100) );
        employList.AddEmployeenew Employee("B","c",  712.5025) );
        employList.AddEmployeenew Employee("C","wt"132.3510) );

        employList.DisplayEmployees( );
        Console.WriteLine("Serialization in progress");
        XmlSerializer s = new XmlSerializertypeofEmployeeList ) );
        TextWriter w = new StreamWriter("employList.xml");
        s.Serializew, 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.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
26. 24. Xml serialization
26. 24. 1. XML serialization with namespace setting
26. 24. 2. Serialize/Deserialize Xml: deal with element list
26. 24. 3. Specify the XmlRoot and XmlAttribute for XML serialization
26. 24. 4. Specify format and indentation for object XML serialization
w___w___w_.j___a_v_a_2_s_.c___om___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.