Simple way to restore object relationships after deserialization?

  • Thread starter Thread starter Xavier J
  • Start date Start date
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'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.
 
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
 
Back
Top