XmlRoot, XmlIgnore and XmlAttribute for XML serialization

 

XmlIgnore: If you specify XmlIgnore on a field, that field will be ignored when serialized.
XmlAttribute:This Specifies that the XmlSerializer must serialize the class member as an XML attribute.
XmlRoot:Controls XML serialization of the attribute target as an XML root element. You can use the word 
XmlRoot in your code instead of the longer XmlRootAttribute.

XmlElementattribute:  Indicates that a public field or property represents an XML element when the XmlSerializer serializes or deserializes the object that contains it. Set the element name and namespace of the XML element.  By applying an XmlElementAttribute to an array, you instruct   the XmlSerializer to serialize the array as a series of XML  elements, instead of a nested set of elements.

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s