使用IXmlSerializable如何实现自定义序列化和默认序列化

时间:2013-11-03 17:04:17

标签: c# xml architecture xml-serialization

我有一个域类,用于将xml序列化数据发送到我需要启用自定义序列化的组件之一

这课。而其他组件想要使用域类对象的默认序列化。

所以我需要在域实体上同时进行默认和自定义xml序列化。所以选择了这种设计方法:

class Program 
{ 
static void Main(string[] args) 
{ 
Book book = new Book(); 
book.Name = "MSDN"; 
book.Price = 28; 
// Comment bellow line to enable default serialization or Uncomment  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);  
            }
        }

请建议我如何在域实体“Book - Here”上实现两个序列化。

0 个答案:

没有答案