Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Serialize C# class to XML

Posted on 2007-04-11
3
Medium Priority
?
933 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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 …
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 …

730 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