XML Serialization of Plain CLR object without using XMLSerializer attribute

My last article was about converting the Objects to XML using XMLAttribute .Here i am trying to show How to convert a object to XML fromat which does not have any attribute.

Following snippet demonstrate it clearly _

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
 
namespace XMLSerlizer
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Vehicle> lstVeh = new List<Vehicle>();
 
            Vehicle v = new Vehicle();
            v.id = 1;
            v.Name = "ZXen";
            v.company = "Maruti";
            lstVeh.Add(v);
 
            Vehicle v1 = new Vehicle();
            v1.id = 1;
            v1.Name = "Fabia";
            v1.company = "Scoda";
            lstVeh.Add(v1);
 
            SerializeObject(lstVeh);
        }
 
        public static void  SerializeObject<T>(T obj)
        {
                XmlSerializer xs = new XmlSerializer(typeof(T));
                XmlWriter wrt = XmlWriter.Create(@"c:\temp.xml");
                xs.Serialize(wrt, obj);
                wrt.Close();  
        }
 
    }
 
    public class Vehicle
    {
        public int id;
 
        public string Name;
 
        public string company;
 
 
    }
}
Ebook Download
View all
Learn
View all