Simple way to restore object relationships after deserialization?

X

Xavier J

I'm working with XML serialization of hierarchical objects such as the
following:

public class OuterClass
{
InnerClass _inner;

public InnerClass Inner
{ get {return _inner;}
set {_inner = value;}
}
}

public class InnerClass
{ OuterClass _parentouter;

[XmlIgnore()]
public OuterClass ParentOuter
{ get {return _parentouter;}
set {_parentouter = value;}
}
}

Okay, so without [XmlIgnore], if an OuterClass instance has property Inner
set, whose ParentOuter points back, then this won't XML serialize because the
serializer will be in an infinite loop between parent and child. Okay, so
I've added [XmlIgnore].

***During*** deserialization, how do I get an InnerClass's OuterParent
property to point back to the parent without? I really want to avoid writing
custom serializers cause I have lots of classes that interrelate like this.

Help!
 
I

Ignacio Machin

I'm working with XML serialization of hierarchical objects such as the
following:

public class OuterClass
{
    InnerClass _inner;

    public InnerClass Inner
    {   get {return _inner;}
        set {_inner = value;}
    }

}

public class InnerClass
{  OuterClass _parentouter;

    [XmlIgnore()]
    public OuterClass ParentOuter
    {   get {return _parentouter;}
        set {_parentouter = value;}
    }

}

Okay, so without [XmlIgnore], if an OuterClass instance has property Inner
set, whose ParentOuter points back, then this won't XML serialize becausethe
serializer will be in an infinite loop between parent and child.  Okay,so
I've added [XmlIgnore].

***During*** deserialization, how do I get an InnerClass's OuterParent
property to point back to the parent without?  I really want to avoid writing
custom serializers cause I have lots of classes that interrelate like this.

Help!

Hi,

You will have to custom serialize your class. Take a look at
IXmlSerializable interface, if you search for it you will find several
examples of how to use it.
 
F

Family Tree Mike

Xavier J said:
I'm working with XML serialization of hierarchical objects such as the
following:

public class OuterClass
{
InnerClass _inner;

public InnerClass Inner
{ get {return _inner;}
set {_inner = value;}
}
}

public class InnerClass
{ OuterClass _parentouter;

[XmlIgnore()]
public OuterClass ParentOuter
{ get {return _parentouter;}
set {_parentouter = value;}
}
}

Okay, so without [XmlIgnore], if an OuterClass instance has property Inner
set, whose ParentOuter points back, then this won't XML serialize because the
serializer will be in an infinite loop between parent and child. Okay, so
I've added [XmlIgnore].

***During*** deserialization, how do I get an InnerClass's OuterParent
property to point back to the parent without? I really want to avoid writing
custom serializers cause I have lots of classes that interrelate like this.

Help!

After deserializing, I think this will work:

foreach (OuterClass oc in OuterClassList)
{
oc.Inner.ParentOuter = oc;
}

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top