Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>having a static base class method for all inheriting classes &amp; maintaining proper polymorphism is difficult. You could however remove the static-ness of the method, and have an InitializeFromXml method which would essentially allow you to populate your class from the xml. Though I typically don't care for public Initialize methods, this tends to be better for polymorphism.</p> <p>here's an example. it's a bit much for a small object like this (and I rarely ever actually use xml serialization, but would load it into an xml document and pull out what I need for deserialization assignments), but when things scale, are inherited, and generally more complex, it allows you to reuse quite a bit more:</p> <pre><code>public class CustomObject { public string AValue { get; set; } public bool BValue { get; set; } protected IXmlConfiguration Config = new CustomObjectConfig( ); public virtual string ToXml( ) { return Config.ToXml( this ); } public virtual void InitializeFromXml( string xml ) { Config.FromXml( xml ); AValue = ((CustomObjectConfig)Config).A; BValue = ((CustomObjectConfig)Config).B; } } public interface IXmlConfiguration { void FromXml( string xml ); string ToXml( object instance ); } [XmlRoot( "CustomObject" )] public class CustomObjectConfig : IXmlConfiguration { [XmlElement( "AValue" )] public string A { get; set; } [XmlAttribute( "bvalue" )] public bool B { get; set; } public void FromXml( string xml ) { byte[] bytes = Encoding.UTF8.GetBytes( xml ); using ( MemoryStream ms = new MemoryStream( bytes ) ) { XmlSerializer xs = new XmlSerializer( typeof( CustomObjectConfig ) ); CustomObjectConfig cfg = (CustomObjectConfig)xs.Deserialize( ms ); A = cfg.A; B = cfg.B; } } public string ToXml( object instance ) { string xml = null; if ( instance is CustomObject ) { CustomObject val = (CustomObject)instance; A = val.AValue; B = val.BValue; using ( MemoryStream ms = new MemoryStream( ) ) { XmlSerializer xs = new XmlSerializer( typeof( CustomObjectConfig ) ); xs.Serialize( ms, this ); ms.Seek( 0, 0 ); byte[] bytes = ms.ToArray( ); xml = Encoding.UTF8.GetString( bytes ); } } return xml; } } </code></pre> <p>The reason I'd favor this approach rather than creating xml serializable objects are because </p> <ol> <li>xml serialization typically requires you structure your class one way, and you will typically then use that class another way.</li> <li>it's a bit easier than having huge stacks of xml include attributes (may be called something different) everywhere which would allow the polymorphic serialization of derived types.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload