Prevent XML Serialization of IEnumerable and ICollection <T> and Inherited Types
NOTE: XMLIgnore is NOT the answer!
OK, therefore, following my question about XML Serialization and inherited types , I started integrating this code into my application that I am working on, stupidly thinking that everything will go well ..
I ran into problems with several classes that I have that implement IEnumerable and ICollection <T>
The problem is that when the XMLSerializer comes to serialization, it treats them as an external property, and instead of using the property we would like (i.e. using our AbstractXmlSerializer), it comes here and crashes (due to a mismatch type), pretty much bringing us back to the square. You cannot decorate these methods with the XmlIgnore attribute, so we cannot stop it this way.
My current solution is to remove the implementation of the interface (in this current application, in which there is no real big deal, I just made the code more beautiful).
Should I assimilate my pride in this and acknowledge that this is not possible? I know that I somehow pushed and got more from the XmlSerializer than I expected from it :)
Edit
I must also add that I am currently working under 2.
Update
I accepted the answer lomaxx . In my scenario, I cannot do this, but I know that this will work. Since they had no other suggestions, I ended up removing the interface implementation from the code.
you can get around this problem by getting the System.RunTime.Serialization dll (this is the .net 3.x assembly) and referencing it from your .net 2.0 application. This works because .net 3.0 executables are compiled to work in the CLS.net 2.0 environment.
Thus, you get access to the DataContractSerliazer, which I used to solve a similar problem, when I wanted to pass ICollection as a parameter to the web service, and xmlserializer did not know how to handle it properly.
If you are interested in using dll.net 3.x in your 2.x application, you can use the DataContractSerializer to solve this problem.
I think the answer comes too late to be useful for your specific application, but maybe there are other people having the same problem.
Suppose you can implement IXmlSerializable on an IEnumerable type to get around this behavior. however, this means that you need to have full control over the serialization process for this type. a simple approach that is not related to XmlReader / XmlWriter, you can write an xml adapter adapter class with the public ctor and public read-write-properties functions of all the data to be serialized, and create a temporary XmlSerializer object for this type inside IXmlSerializable .. [Read | Write] Xml ()
class Target : IEnumerable<Anything>, IXmlSerializable { //... public void ReadXml(System.Xml.XmlReader reader) { reader.ReadStartElement(); TargetXmlAdapter toRead = (TargetXmlAdapter)new XmlSerializer(typeof(TargetXmlAdapter)).Deserialize(reader); reader.Read(); // here: install state from TargetXmlAdapter } public void WriteXml(System.Xml.XmlWriter writer) { // NOTE: TargetXmlAdapter(Target) is supposed to store this' state being subject to serialization new XmlSerializer(typeof(TargetXmlAdapter)).Serialize(writer, new TargetXmlAdapter(this)); } } If you use these attributes:
[XmlArray("ProviderPatientLists")] [XmlArrayItem("File")] public ProviderPatientList Files { get { return _ProviderPatientLists; } set { _ProviderPatientLists = value; } } Where ProviderPatientList inherits List<PatientList>
Then you can have more control over the output xml will create