Injecting IXmlSerializable in a generated class that has an XmlTypeAttribute
Basically the original problem is to make the boolean serialized as 0 or 1. The solution I found is to implement IXmlSerializable, which I did. Unfortunately, the class I'm trying to serialize generates code from the schema and has an XmlTypeAttribute on it. When I try to (de) serialize an object with the XmlSerializer created normally ( new XmlSerializer(type)
), it throws this exception:
System.InvalidOperationException: Only XmlRoot attribute may be specified for the type ______ Please use XmlSchemaProviderAttribute to specify schema type.
Two points immediately come to mind:
1) remove the attribute in the generated code. This change needs to be done every time the code has been generated.
2) Use XmlAttributeOverrides object when creating serializer to remove attribute. This will require the rest of the codebase to "know" that it needs to override this attribute. In addition, the exception thrown does not give an absolute idea of what needs to be done to fix it.
Both options seem to stink. Is there a third option?
a source to share
I have the same problem as I am removing IXMLSerializable works, I am not using it and have you tried to hide true or false with some property logic? Like this:
private bool mblnFlag;
public String Flag
{
get
{
return mblnFlag;
}
set
{
mblnFlag = (value == "1")
}
}
Of course, you have to improve the properties and do more validation, but this idea.
a source to share