static void Main(string[] args) { XmlTextWriterFull writer = new XmlTextWriterFull(Console.Out); XmlSerializer xs = new XmlSerializer(typeof(temp)); xs.Serialize(writer, new temp()); Console.ReadKey(); }
/// <summary> /// Serializes an object to XML /// </summary> /// <param name="objectToSerialize">The object to serialize</param> /// <returns>A MemoryStream containing the XML</returns> private static MemoryStream SerializeXmlObject(object objectToSerialize) { // Create a new XML Serializer XmlSerializer serializer = new XmlSerializer(objectToSerialize.GetType()); // Initialize stream and serialize var stream = new MemoryStream(); var writer = new XmlTextWriterFull(stream); try { serializer.Serialize(writer, objectToSerialize); } catch { // Return null if the operations fails return(null); } // Return the stream containing the XML return(stream); }