<p><strong>NOTE: <em>XMLIgnore</em> is NOT the answer!</strong></p>
<p>OK, so following on from my question on <a href="https://stackoverflow.com/questions/20084/xml-serialization-and-inherited-types">XML Serialization and Inherited Types</a>, I began integrating that code into my application I am working on, stupidly thinking all will go well..</p>
<p>I ran into problems with a couple of classes I have that implement <em>IEnumerable</em> and <em>ICollection&lt;T&gt;</em></p>
<p>The problem with these is that when the XMLSerializer comes to serialize these, it views them as an external property, and instead of using the property we would like it to (i.e. the one with our <em>AbstractXmlSerializer</em> ) it comes here and falls over (due to the type mismatch), pretty much putting us back to square one. You cannot decorate these methods with the <strong>XmlIgnore</strong> attribute either, so we cannot stop it that way.</p>
<p>My current solution is to remove the interface implementation (in this current application, its no real big deal, just made the code prettier).</p>
<p><strong>Do I need to swallow my pride on this one and accept it cant be done?</strong> I know I have kinda pushed and got more out of the XmlSerializer than what was expected of it :)</p>
<hr />
<h3>Edit</h3>
<p>I should also add, I am currently working in framework 2.</p>
<hr />
<h3>Update</h3>
<p>I have accepted <a href="https://stackoverflow.com/questions/31799/preventing-xml-serialization-of-ienumerable-and-icollectiont-inherited-types#31810">lomaxx's answer</a>. In my scenario I cannot actually do this, but I do know it will work. Since their have been no other suggestions, I ended up removing the interface implementation from the code.</p>

[{'answer_id': 31810, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 3, 'selected': True, 'text': "<p>you can get around this problem by getting hold of the System.RunTime.Serialization dll (it's a .net 3.x assembly) and referencing it from your .net 2.0 application. This works because the .net 3.0 binaries are compiled to run on the .net 2.0 CLR.</p>\n\n<p>By doing this, you get access to the DataContractSerliazer which I've used to get around a similar problem where I wanted to pass in a ICollection as a parameter to a webservice and the xmlserializer didn't know how to deal with it properly.</p>\n\n<p>If you're cool with using the .net 3.x dll in your 2.x application you should be able to use the DataContractSerializer to solve this problem</p>\n"}, {'answer_id': 31824, 'author': 'Brian Leahy', 'author_id': 580, 'author_profile': 'https://Stackoverflow.com/users/580', 'pm_score': 1, 'selected': False, 'text': '<p>If you use these attributes:</p>\n\n<pre><code>        [XmlArray("ProviderPatientLists")]\n        [XmlArrayItem("File")]\n      public ProviderPatientList Files\n    {\n        get { return _ProviderPatientLists; }\n        set\n        {\n            _ProviderPatientLists = value;\n        }\n    }\n</code></pre>\n\n<p>Where ProviderPatientList  inherit\'s <code>List&lt;PatientList&gt;</code></p>\n\n<p>You can then have more control over the xml outputed\nwill create </p>\n\n<p>\n  \n  \n  \n  \n  \n  \n</p>\n'}, {'answer_id': 1048455, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>i guess the answer is coming too late to be useful for your particular application, but maybe there are other people having the same problem.</p>\n\n<p>i suppose you can implement IXmlSerializable for the IEnumerable type to work around this behaviour. however, this means you have to fully control the serialization process for this type. a simple approach for not having to mess with the XmlReader / XmlWriter, you can write a helper xml adapter class with public ctor and public read-write-properties of all the data to be serialized, and create a temporary XmlSerializer object for this type inside IXmlSerializable.[Read|Write]Xml().</p>\n\n<pre><code>class Target : IEnumerable&lt;Anything&gt;, IXmlSerializable\n{\n//...\n\npublic void ReadXml(System.Xml.XmlReader reader)\n{\n    reader.ReadStartElement();\n    TargetXmlAdapter toRead = (TargetXmlAdapter)new XmlSerializer(typeof(TargetXmlAdapter)).Deserialize(reader);\n    reader.Read();\n\n    // here: install state from TargetXmlAdapter\n}\n\npublic void WriteXml(System.Xml.XmlWriter writer)\n{\n    // NOTE: TargetXmlAdapter(Target) is supposed to store this' state being subject to serialization\n    new XmlSerializer(typeof(TargetXmlAdapter)).Serialize(writer, new TargetXmlAdapter(this));\n}\n}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/31799', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/832/']