Tips on XmlSerialization

To support default XmlSerialization with .NET XmlSerializer a class must to:

  • include constructor without parameters
  • all public properties
    • that should be saved must have public setter
    • if it shouldn’t be saved then must have [XmlIgnore] attribute

Class example:

public class dataForXML {

public dataForXML() {
}

private String _bProperty;

// this we don't want to be saved
[XmlIgnore]
public String bProperty {
get {
return _bProperty;
}
set {
_bProperty = value;
}
}


private String _aProperty;
// this property we want to save
public String aProperty {
get {
return _aProperty;
}

set {
_aProperty = value;
}
}

}

 

Spread the love