C#部分反序列化

Car*_*rlo 6 c# serialization

所以我有一个xml,它具有与此类似的结构:

<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>
Run Code Online (Sandbox Code Playgroud)

有没有办法可以反序列化MyObject而不是ArrayOfOtherObject?然后在需要时做一个懒惰的ArrayOfOtherObject加载?

我通常使用XmlDeserialization,但AFAIK总是加载整个东西.

谢谢!

Vit*_*sky 2

您可以使用二进制反序列化功能识别的特殊构造函数:

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
}
Run Code Online (Sandbox Code Playgroud)

对于 XML - 以下是自定义序列化的示例: 链接