locked
Using "IXmlSerializable" - how can i achieve both default / normal xmlserialization and custom/conditional xmlerialization ? RRS feed

  • Question

  •  I have a domain class, for sending the xml serialized data to one of the component I need to enable custom serialization for

    the class. while the other components wants to use default serialization with the domain class object.

    So I need to have both default and custom xml serialization on a domain Entity. So choosed this design approach :

    Example :

    class Program { static void Main(string[] args) { Book book = new Book(); book.Name = "MSDN"; book.Price = 28;

    // Comment or Uncomment below line to enable default/custom serialization.

    book.CustomSerializer = new CustomBookSerializer(book); XmlSerializer xs = new XmlSerializer(book.GetType()); using (StringWriter textWriter = new StringWriter()) { xs.Serialize(textWriter, book); Console.WriteLine(textWriter.ToString()); } } } public interface ICustomSerializer { XmlSchema GetSchema(); void ReadXml(XmlReader reader); void WriteXml(XmlWriter writer); } public class CustomBookSerializer : ICustomSerializer { private Book book; public CustomBookSerializer(Book book) { this.book = book; } public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { } public void WriteXml(XmlWriter writer) { writer.WriteStartElement("<Book>"); addPropertyElement(writer, "Name", "ID", "Name", this.book.Name); if (this.book.Language == Language.English) { addPropertyElement(writer, "Price", "ID", "Price", this.book.Price.ToString()); } writer.WriteEndElement(); } private void addPropertyElement(XmlWriter writer, string propertyElement, string iDAttribute, string iDAttributeValue, string elementValue) { if (!string.IsNullOrEmpty(elementValue)) { writer.WriteStartElement(propertyElement); writer.WriteAttributeString(iDAttribute, iDAttributeValue); writer.WriteString(elementValue); writer.WriteEndElement(); } } } public enum Language { English, French, German } public class Book : IXmlSerializable { public string Name { get; set; } public int Price { get; set; } public Language Language { get; set; } public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { } private ICustomSerializer customSerializer; public ICustomSerializer CustomSerializer { get { return this.customSerializer; } set { this.customSerializer = value; } } private bool isDefaultSerialization = false; public void WriteXml(XmlWriter writer) { if (isDefaultSerialization) { isDefaultSerialization = false; return; } if (this.customSerializer != null) { this.customSerializer.WriteXml(writer); } else { // Use Default Serialization. ---------------- How can i achive this default serialization, without removing implemenation of IXmlSerializable isDefaultSerialization = true; var defalutSerializer = new XmlSerializer(typeof(Book)); defalutSerializer.Serialize(writer, this); } } }


      Please suggest me how to achieve both serialization on the Domain Entity "Book - Here".

     

     Thanks & Regards,

    Manoranjan

     



    Tuesday, July 2, 2013 12:06 PM