Solved

Serialize C# class to XML

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

734 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