Solved

Serialize C# class to XML

Posted on 2007-04-11
3
898 Views
Last Modified: 2013-11-26
I have the following class in C#:

public class Employee
{
   public string Name;
   public int  Age;
   public string Phone;
}

and supposing i have an array of Employee objects, what can i do to xml-serialize this class like:

<Employees>
   <Employee>
     <Name>John Smith</Name>
     <Age>23</Age>
     <Phone>1 800 12121212</Phone>
   </Employee>
   <Employee>
     <Name>Bruce</Name>
     <Age>89</Age>
     <Phone>1 800 23766767</Phone>
   </Employee>
   <Employee>
     <Name>Allan Poe</Name>
     <Age>45</Age>
     <Phone>91228 21812812</Phone>
   </Employee>
</Employees>

0
Comment
Question by:jlbo_davinchi
  • 2
3 Comments
 
LVL 3

Expert Comment

by:JipFromParis
ID: 18891019
Use appropriate attributes from the System.Runtime.Serialization namespace.
0
 
LVL 12

Accepted Solution

by:
gbzhhu earned 50 total points
ID: 18891182
JipFromParis was in the right direction but I don't think he/she explained enough

Your array should be declared

            [XmlArrayItem(ElementName= "Employee", IsNullable=false, Type = typeof(Employee))]
            [XmlArray(ElementName= "Employees")]
            public ArrayList Employees
            {
                  get { return this.employee; }
                  set { this.employee = value; }
            }      

Next the class containing the array must be decorated with the following attributes (just before the class declaration)

      [Serializable]
      [System.Xml.Serialization.XmlInclude( typeof( Employee) )]      

Last use the following function in the class containing the array to save serialise the array

public bool Save()
{
      bool saved = false;
      StreamWriter file = null;

      try
      {
            // Serialize the object to Xml.
            XmlSerializer writer = new XmlSerializer(this.GetType());

            file = new StreamWriter(@"c:\employees.xml");


            // Write the serialized object out to a file.
            writer.Serialize(file, this);
            saved = true;
      }
      catch (Exception ex)
      {
            saved = false;
            //Do something - log error
      }
      finally
      {
            if (file != null)
            {
                  // Make sure we always close the file.
                  file.Close();
            }
      }

      return saved;
}

 
0
 
LVL 12

Expert Comment

by:gbzhhu
ID: 18891202
Don't forget to declare the array employee as private otherwise the public property won't work.  I am sure you know that
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question