C # Partial Deserialization

So, I have an xml that has a similar structure to this:

<MyObject> <PropertyA>Value</PropertyA> <PropertyB>Value</PropertyB> <PropertyC>Value</PropertyC> <ArrayOfOtherObject> <OtherObject> <PropertyX>Value</PropertyX> <PropertyY>Value</PropertyY> <PropertyZ>Value</PropertyZ> </OtherObject> <OtherObject> <PropertyX>Value</PropertyX> <PropertyY>Value</PropertyY> <PropertyZ>Value</PropertyZ> </OtherObject> <OtherObject> <PropertyX>Value</PropertyX> <PropertyY>Value</PropertyY> <PropertyZ>Value</PropertyZ> </OtherObject> </ArrayOfOtherObject> </MyObject> 

Is there a way to deserialize MyObject but not ArrayOfOtherObject? And then do lazy loading of ArrayOfOtherObject later if necessary?

I usually use XmlDeserialization, but AFAIK always loads it all.

Thanks!

+6
c # serialization
source share
2 answers

You can use a special constructor that is recognized by the binary deserialization function:

 protected MyObject(SerializationInfo info, StreamingContext context) { //here some elements you can load right now, and some other to store in so-to-say string in order to load later } 

In the case of XML, here is an example of custom serialization: http://geekswithblogs.net/marcel/archive/2006/05/19/78989.aspx

+2
source share

Are you talking about xml deserialization because it is parsed so you don’t need to load the whole XML file into memory or deserialize it when trying to access a specific object?

This may help consider SAX implementation rather than DOM:

http://www.saxproject.org/

0
source share

All Articles