views:

315

answers:

3

I have a class that parses very large file (that can't fit in memory) and I'm currently utilizing the IEnumerable interface to use foreach so I can easily grab the parsed contents of the file line by line. Currently, I'm trying to write this to file using an XMLSerializer. It insists on enumerating the class and in my case, this means dumping large, parsed content of files to the XML. Is there anyway (hackish or elegant) to use this interface and still get the "expected" functionality out of a XMLSerializer?

(I only call this "expected" because this seems an expected outcome if I was implementing some collection interface rather than IEnumerable. Perhaps my concepts of IEnumerable and collections is all out of whack. :( )

A: 

It seems to me that your problem could be solved by using tighter encapsulation.

It sounds as if you are using the same class to load your data from file, and to store the in-memory representation of that data. If this is the case, you could (and possibly should) move the functionality into two classes. Then only serialize the 'in-memory' class.

andypaxo
+2  A: 

Well, you could implement IXmlSerializable and seize full control. It isn't entirely clear (without pseudo code) what the setup is - sometimes [XmlIgnore] can help, but I'm not sure in this case without an example of what you have, and what you do/don't want.

Marc Gravell
Cheers for the edit Jon; fat fingers today ;-p
Marc Gravell
+1  A: 

I managed to fix this problem without having to change my design. None of my code was relying on the IEnumerable interface, just the implementation of IEnumerable GetEnumerator() (apparently foreach doesn't check to see if IEnumerable is implemented). Just commenting out the interface in the class declaration did the trick.

llamaoo7
Brilliant solution. I was having this same problem, where I only needed enumerators for convenience. By dropping the interface implementation (but still providing my enumerators) my serialization works again and I can still use foreach.
phasetwenty