C# Partial deserialization

放肆的年华 提交于 2019-12-23 10:00:30

问题


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!


回答1:


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




回答2:


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/



来源:https://stackoverflow.com/questions/1572999/c-sharp-partial-deserialization

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!