Solved

Serialize C# class to XML

Posted on 2007-04-11
3
878 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
Comment Utility
Use appropriate attributes from the System.Runtime.Serialization namespace.
0
 
LVL 12

Accepted Solution

by:
gbzhhu earned 50 total points
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now