CSharp examples for System.Xml:XML Serialization
Serialize Collection To XML File
using System.IO;//from ww w. j a v a 2 s .c om using System.Text; using System.Xml.Serialization; using System.Xml; using System; public class Main{ public static void SerializeCollectionToFile(object oObject, string sFilename, string sRootElementName) { try { XmlRootAttribute ra = new XmlRootAttribute(); ra.Namespace = string.Empty; ra.ElementName = sRootElementName; System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(oObject.GetType(), ra); XmlTextWriter xmlWriter = new XmlTextWriter(sFilename, Encoding.Unicode); xmlSerializer.Serialize(xmlWriter, oObject); xmlWriter.Close(); } catch (Exception exp) { throw new XMLSerializerException(string.Format("Error saving object to file: {0}", exp.Message), exp); } } }