I don't see a reason why anyone would want to override these methods: don't do that, simply let the framework serialize the data.
Well you did ask...
I wanted to override the methods for several reasons:
1) It serializes too much. ReadOnly properties should not have be serialized by default IMHO. You make a valid point about it actually being an XMLDocument that could be used for purposes other than serialization but an option on XmlFormatAspect to turn that on when required would have been better. As you say too late to change that now.
2) Your point about attributes based on project settings doesn't apply here. Serializing the generated entities is (I assume) all fine but, for actual usage, I have partial classes for virtually all entities and none of the additional properties on these have attributes because they weren't necessary until now.
3) It serializes properties with no value - e.g. "<LatestValuation></LatestValuation>"
4) It serializes properties where there is no prospect of deserialization (or can have no use in an XmlDocument)
e.g.
[Browsable(false), XmlIgnore]
public PartyListNameFormatter PartyListNameFormatter { get; set; }
is on several of my entities. It serializes as "<PartyListNameFormatter>TIPS.DAL.EntityClasses.PartyListNameFormatter</PartyListNameFormatter>" and, worse, bombs on deserialization because a string cannot be converted back to a PartyListNameFormatter.
5) I don't want to change the Xml format per se - Compact25 is fine. I am an old man now and way too old to be dealing with serialization again! I just wanted a bit of control so that I could prevent serializing that which didn't need to be serialized.
If this code in Entity2Xml:
else if (!uniqueFieldNames.Contains(properties[index].Name))
{
writer.WriteStartElement(properties[index].Name);
XmlHelper.WriteValueAsStringToXml(properties[index].PropertyType, properties[index].GetValue((object) this), verboseXml, !flag2, writer, datesInXmlDataType, mlInCDataBlocks);
writer.WriteEndElement();
}
had just called a virtual bool ShouldXmlSerialize(PropertyDescriptor) method and skipped if false is returned then I could have done everything I need in there.
As it is, I now have to visit 113 partial entity classes (plus at least 1 template) to add XmlIgnore attributes on hundreds of properties.
My biggest problem is item 4 - if I miss an attribute accidentally now or in the future and it gets serialized then it is not possible to read that graph back again.