Blog Post : Using XmlSerializer with external serializable types
Listing 1 : Simple extensible serializable and serialization support class source code
  1. namespace ExtData  
  2. {  
  3.     using System;  
  4.     using System.IO;  
  5.     using System.Text;  
  6.     using System.Collections;  
  7.     using System.Xml;  
  8.     using System.Xml.Schema;  
  9.     using System.Xml.Serialization;  
  10.       
  11.     [System.Xml.Serialization.XmlRootAttribute("ExtensibleData", Namespace="", IsNullable=false)]  
  12.     public partial class ExtensibleDataType  
  13.     {  
  14.         private string name;  
  15.         private string description;  
  16.         private object externData;  
  17.         private string id;  
  18.         [System.Xml.Serialization.XmlElementAttribute("Name")]  
  19.         public string Name  
  20.         {  
  21.             get { return name; }   
  22.             set { name = value; }  
  23.         }  
  24.         [System.Xml.Serialization.XmlElementAttribute("Description")]  
  25.         public string Description  
  26.         {  
  27.             get { return description; }  
  28.             set { description = value; }  
  29.         }  
  30.         [System.Xml.Serialization.XmlElementAttribute("ExternData")]  
  31.         public object ExternData  
  32.         {  
  33.             get { return externData; }  
  34.             set { externData = value; }  
  35.         }  
  36.         [System.Xml.Serialization.XmlAttributeAttribute("ID")]  
  37.         public string ID  
  38.         {  
  39.             get { return id; }  
  40.             set { id = value; }  
  41.         }  
  42.         public virtual string Xml  
  43.         {  
  44.             get { return Serializer.ToXml(thisthis.GetType()); }  
  45.         }  
  46.         public static ExtensibleDataType FromXml(string xml)  
  47.         {  
  48.             return ((ExtensibleDataType)(Serializer.FromXml(xml, typeof(ExtensibleDataType))));  
  49.         }  
  50.         [System.Xml.Serialization.XmlAttributeAttribute(Namespace = "http://www.w3.org/2001/XMLSchema-instance")]  
  51.         public virtual string noNamespaceSchemaLocation  
  52.         {  
  53.             get { return "ExtData.xsd"; }  
  54.             set {}  
  55.         }  
  56.     }  
  57.     public class Serializer  
  58.     {  
  59.         public static string ToXml(object obj, Type type)  
  60.         {  
  61.             XmlSerializer serializer = new XmlSerializer(type);  
  62.             MemoryStream stream = new MemoryStream();  
  63.             UTF8Encoding encoding = new UTF8Encoding(false);  
  64.             XmlTextWriter writer = new XmlTextWriter(stream, encoding);  
  65.             if (PrettyPrint)  
  66.             {  
  67.                 writer.Formatting = Formatting.Indented;  
  68.                 writer.Indentation = 1;  
  69.                 writer.IndentChar = Convert.ToChar(9);  
  70.             }  
  71.             serializer.Serialize(writer, obj);  
  72.             string xml = encoding.GetString(stream.GetBuffer(), 0, (int)stream.Length);  
  73.             writer.Close();  
  74.             stream.Close();  
  75.             return xml;  
  76.         }  
  77.         public static object FromXml(string xml, Type type)  
  78.         {  
  79.             XmlSerializer serializer = new XmlSerializer(type);  
  80.             MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));  
  81.             object obj = serializer.Deserialize(stream);  
  82.             stream.Close();  
  83.             return obj;  
  84.         }  
  85.         private static bool prettyPrint;  
  86.         public static bool PrettyPrint  
  87.         {  
  88.             get { return prettyPrint; }  
  89.             set { prettyPrint = value; }  
  90.         }  
  91.     }  
  92. }