views:

260

answers:

2

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 that I can deserialize MyObject but not the ArrayOfOtherObject? And then later on do a lazy load of ArrayOfOtherObject when needed?

I usually use XmlDeserialization, but AFAIK it always loads the whole thing.

Thanks!

+2  A: 

Hello,

You can use special constructor which is recognized by binary deserialization functionality:

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 case of XML - here is an example of custom serialization: http://geekswithblogs.net/marcel/archive/2006/05/19/78989.aspx

Vitaliy Liptchinsky
Isn't that `BinaryFormetter`? xml is typically `IXmlSerializable`, which looks *very* different.
Marc Gravell
Yes, Mark, you are right but IXmlSerializable can also easily be extended through ReadXml method.
Vitaliy Liptchinsky
So how would I go about this? I'm really noob with this, since I've only done the regular deserialization (deserialize entire object) and only used XmlIgnore. I searched on the MSDN how to use this but it was rather complicated and could not getting to work (sometimes I hate MSDN for their overly complicated examples).
Carlo
Here is an example of custom serialization:http://geekswithblogs.net/marcel/archive/2006/05/19/78989.aspx
Vitaliy Liptchinsky
Great, thank you. I'll take a look.
Carlo
A: 

Are you talking about deserializing the xml as it is parsed so that you don't have to load the entire xml file into memory, or deserializing it as you try to access the concrete object?

It might help to look at an implementation of SAX as opposed to DOM:

http://www.saxproject.org/

PabloC
I need to deserialize the MyObject but not the ArrayOfOtherObject. And later on if requested by the user, I'll need to load the ArrayOfOtherObject, but initially only the MyObject.
Carlo
Another thought is that an RDBMS would be better suited to storing and retrieving this data in the manner you require. You could import this data in to a relational database and then query it using LINQ to SQL for example (which uses lazy loading as I recall).
PabloC