Difference between revisions of "C"

From Richard's Wiki
Jump to: navigation, search
(SerializeToXml)
(SerializeToXml)
Line 1: Line 1:
 
* [http://elegantcode.com/2009/07/03/wpf-multithreading-using-the-backgroundworker-and-reporting-the-progress-to-the-ui/ Using the BackgroundWorker and Reporting the Progress to the UI]
 
* [http://elegantcode.com/2009/07/03/wpf-multithreading-using-the-backgroundworker-and-reporting-the-progress-to-the-ui/ Using the BackgroundWorker and Reporting the Progress to the UI]
  
==== SerializeToXml ====
+
===== SerializeToXml =====
  
 
         #using System.IO;
 
         #using System.IO;

Revision as of 16:58, 14 June 2011

SerializeToXml
       #using System.IO;
       #using System.Xml;
       /// <summary>
       /// Serializes to XML.
       /// </summary>
       /// <param name="firstClassElement">The first class element to serialize.</param>
       /// <returns>The XML string that represents the passed first class element.</returns>
       public virtual string SerializeToXml(FirstClassElementType firstClassElement)
       {
           string xmlData;
           var memoryStream = new MemoryStream();
           XmlTextWriter writer = null;
           try
           {
               writer = new XmlTextWriter(memoryStream, Encoding.Unicode)
                            {
                                Formatting = Formatting.Indented,
                                Indentation = 0
                            };
               var xmlSerializer = new XmlSerializer(typeof (FirstClassElementType));
               xmlSerializer.Serialize(writer, firstClassElement);
               writer.Flush();
           }
           finally
           {
               memoryStream.Seek(0, SeekOrigin.Begin);
               var textReader = new StreamReader(memoryStream);
               xmlData = textReader.ReadToEnd();
               textReader.Close();
               if (null != writer)
                   writer.Close();
           }
           return xmlData;
       }