6

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.

2
  • 2
    To be honest, unless the data volume is huge you would be better off going for the simplest approach, and deserialize it all. Commented Oct 15, 2009 at 15:39
  • +1 good question Carlo Commented Dec 9, 2013 at 16:33

2 Answers 2

2

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: Link

Sign up to request clarification or add additional context in comments.

4 Comments

Isn't that BinaryFormetter? xml is typically IXmlSerializable, which looks very different.
Yes, Mark, you are right but IXmlSerializable can also easily be extended through ReadXml method.
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).
Here is an example of custom serialization: geekswithblogs.net/marcel/archive/2006/05/19/78989.aspx
0

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/

2 Comments

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.
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.