Solved

Serialize C# class to XML

Posted on 2007-04-11
3
885 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is about my first experience with programming Arduino.
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…

813 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

19 Experts available now in Live!

Get 1:1 Help Now