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?
source to share
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.
source to share