I have created a couple of classes mean to represent a relational data structure ( parent child structures ). Below is an example of XML representation so far giving you an idea of what I mean
<BillingFile>
<Account>
<acctnum>122344231414</acctnum>
<adjustments>34.44</adjustments>
<Charges>
<lineitem>
<chargetype>PENALTY</chargetype>
<amount>40.50</amount>
<ratecode>E101</ratecode>
</lineitem>
<lineitem>
<chargetype>LATE CHARGE</chargetype>
<amount>445.35</amount>
<ratecode>D101</ratecode>
</lineitem>
</Charges>
</Account>
</BillingFile>
What I’m doing with my application is parsing through a large text file which could have upwards of 50,000+ accounts in it. Each time an account is read, I will create an “Account” object that has the parent objects, etc. The end goal is to be able to create an XML file containing all this account info that is serialized from the objects created.
The problem I see with this, is that if I store all these objects in memory it will cause a performance issue as it runs in those 50k+ record files.
What I’m wondering is, is there a way to sequentially serialize an object in C#, rather than all at once?
I’ve done some googling and it seems that the built in serialization methods of .NET are a one and done kind of deal. Is there a better way I can do this?
I’d rather avoid having to do any intermediate steps like storing the data in a database, since it’s easier to modify code than it is to mess with a bunch of tables and JOIN statements.
Thoughts?
XmlSerializer.Deserializetakes anXmlReaderparameter. You could place theXmlReaderjust at the<Account>tag, and call theXmlSerializerthere.Similarly for serialization
You could also create a
BillingFileclass that implementsIXmlSerializable, and put this functionality there.Or if you prefer a push-based model instead: