How can I use the IEnumerable interface and at the same time, so that the XMLSerializer doesn't use GetEnumerator ()?

I have a class that parses a very large file (which cannot fit into memory) and I am currently using the IEnumerable interface to use foreach, so I can easily get the parsed file contents line by line. I am currently trying to write this to a file using XMLSerializer. It insists on enumerating the class, and in my case that means dumping large, parsed file content into XML. Is there anyway (hacky or elegant) to use this interface and still get the "expected" functions from the XMLSerializer?

(I call this "expected" because it seems to be the expected result if I implement some interface for a collection rather than IEnumerable. Perhaps my concepts of IEnumerable and collections are all because of this. :()

0


source to share


3 answers


I managed to solve this problem without changing my design. None of my codes relied on the IEnumerable interface, just the IEnumerable GetEnumerator () implementation (obviously foreach doesn't check if IEnumerable is implemented). Just commenting out the interface in the class declaration did the trick.



+2


source


Well, you can implement IXmlSerializable

and take full control. It's not entirely clear (no pseudocode) what a setting is - [XmlIgnore]

can sometimes help, but I'm not sure in this case without an example of what you have and what you do / don't want.



+3


source


It looks to me like your problem could be solved with tighter encapsulation.

It sounds like you are using the same class to load data from a file and to store data in memory for that data. If so, you can (and probably should) translate the functionality into two classes. Then just serialize the "in-memory" class.

0


source







All Articles