Proper way to implement IXmlSerializable?

前端 未结 5 474
逝去的感伤
逝去的感伤 2020-11-22 05:34

Once a programmer decides to implement IXmlSerializable, what are the rules and best practices for implementing it? I\'ve heard that GetSchema() sh

5条回答
  •  孤独总比滥情好
    2020-11-22 06:34

    If you already have an XmlDocument representation of your class or prefer the XmlDocument way of working with XML structures, a quick and dirty way of implementing IXmlSerializable is to just pass this xmldoc to the various functions.

    WARNING: XmlDocument (and/or XDocument) is an order of magnitude slower than xmlreader/writer, so if performance is an absolute requirement, this solution is not for you!

    class ExampleBaseClass : IXmlSerializable { 
        public XmlDocument xmlDocument { get; set; }
        public XmlSchema GetSchema()
        {
            return null;
        }
        public void ReadXml(XmlReader reader)
        {
            xmlDocument.Load(reader);
        }
    
        public void WriteXml(XmlWriter writer)
        {
            xmlDocument.WriteTo(writer);
        }
    }
    

提交回复
热议问题