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 _
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
namespaceXMLSerlizer
{
classProgram
{
staticvoidMain(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);
}
publicstaticvoidSerializeObject<T>(T obj)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
XmlWriter wrt = XmlWriter.Create(@"c:\temp.xml");
xs.Serialize(wrt, obj);
wrt.Close();
}
}
publicclassVehicle
{
publicint id;
publicstring Name;
publicstring company;
}
}
0 comments
Comments
Join the conversation! Your thoughts help the community grow.
Join to leave a comment
Members earn 10 SHARP for a comment, 10 SHARP for a reaction, and 250 SHARP for every blog they publish.
Join the conversation! Your thoughts help the community grow.