C # works for XmlSerializer.Deserialize pitfall?

I'm just wondering if there are any good workarounds for deserializing private fields / properties using XmlSerializer.Deserialize ()?

I am currently deserializing my XML into a simple disposable type with all public properties, then loading a complex type that has private properties like:

ComplexType complex = new ComplexType(SimpleType);

      

and the ComplexType constructor looks like this:

public ComplexType(SimpleType simpleType){
    this.Property1 = simpleType.Property1;
    this.Property2 = simpleType.Property2;
    .....

}

      

Does anyone have a better way to do this?

0


source to share


2 answers


You can have ComplexType implement the IXmlSerializable interface. This provides serialization and deserialization methods, so you can populate the private members of the complextype in those methods.



Check out MSDN here: http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx for an example demonstrating the implementation of the IXmlSerializable interface that serializes a private field.

+1


source


Note that another option is to use DataContractSerializer

(.NET 3.0) - this supports serialization of private members (properties or fields).



+1


source







All Articles