i have class User and i have a List Collection List
The user class contain the following:
public class User
{
public property string userCode{get;set;}
public property string userName{get;set;}
}
How do i convert the list collection to XML?
thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vasanth R16 reputationPosted Sep 4, 2010, 3:12 AM EDT
ShankeyRank #972 · 2.1K reputationPosted Sep 4, 2010, 3:28 AM EDT
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] public class User { public property string userCode{get;set;} public property string userName{get;set;} }/// summary> /// Returns the xml as string based on user object values /// /summary> /// param name="user">object that would be converted into xml/param> /// returns>/returns> public string UserXml(User user) { MemoryStream stream = null; TextWriter writer = null; try { stream = new MemoryStream(); // read xml in memory writer = new StreamWriter(stream, Encoding.Unicode) ; // get serialise object XmlSerializer serializer = new XmlSerializer(typeof(user)); serializer.Serialize(writer, user); // read object int count = (int) stream.Length; // saves object in memory stream byte[] arr = new byte[count]; stream.Seek(0, SeekOrigin.Begin); // copy stream contents in byte array stream.Read(arr, 0, count); UnicodeEncoding utf = new UnicodeEncoding(); // convert byte array to string return utf.GetString(arr).Trim(); } catch { return string.Empty; } finally { if(stream != null) stream.Close(); if(writer != null) writer.Close(); } }Sam HobbsRank #107 · 20.5K reputationPosted Sep 4, 2010, 7:47 PM EDT