XML Serialization, collection with parental links

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oleg.Ogurok@gmail.com

    XML Serialization, collection with parental links

    Hi there,

    I'm writing a custom collection, which will be referened within a
    class. Each item within the collection needs to have a link to the
    parent container class, e.g.

    Item x;
    Container c = x.ParentContain er;

    I basically got the idea from ControlCollecti on class, where each
    control has a reference to its parent control.
    So far, I've implemented it as follows.

    class Container
    {
    public ItemCollection Items { get; }
    }

    class ItemCollection
    {
    public ItemCollection( Container container) { .. }
    public void Add(Item item) { _innerCollectio n.Add(item);
    item.SetContain er(_container); }
    }

    class Item
    {
    // gets the reference to the parent container
    public Container ParentContainer { get; }
    }

    I need to be able to serialize the Container class and all its members
    into XML, and then deserialize it. Upon deserialization , I'm able to
    get the values back into the collection. However, the links to the
    parent container are not preserved.
    I've read that .NET XML serializer doesn't work with circular
    references. Perhaps this could be the reason.

    Is there a better implementation?
    If not, is there a way to relink the parent elements manually upon
    deserialization ?

    Thanks,
    -Oleg.

  • simida

    #2
    Re: XML Serialization, collection with parental links

    i don't how to relink the parent elements upon deserailization . but i
    think after deserailzation process , you can manually use this to
    relink relation.

    Class Continer
    {
    public void Relink()
    {
    foreach (Item item in this.Items)
    {
    item.SetContain er(this);
    }
    }
    }

    Sincerely,
    simida

    Oleg.Ogurok@gma il.com 写道:
    Hi there,
    >
    I'm writing a custom collection, which will be referened within a
    class. Each item within the collection needs to have a link to the
    parent container class, e.g.
    >
    Item x;
    Container c = x.ParentContain er;
    >
    I basically got the idea from ControlCollecti on class, where each
    control has a reference to its parent control.
    So far, I've implemented it as follows.
    >
    class Container
    {
    public ItemCollection Items { get; }
    }
    >
    class ItemCollection
    {
    public ItemCollection( Container container) { .. }
    public void Add(Item item) { _innerCollectio n.Add(item);
    item.SetContain er(_container); }
    }
    >
    class Item
    {
    // gets the reference to the parent container
    public Container ParentContainer { get; }
    }
    >
    I need to be able to serialize the Container class and all its members
    into XML, and then deserialize it. Upon deserialization , I'm able to
    get the values back into the collection. However, the links to the
    parent container are not preserved.
    I've read that .NET XML serializer doesn't work with circular
    references. Perhaps this could be the reason.
    >
    Is there a better implementation?
    If not, is there a way to relink the parent elements manually upon
    deserialization ?

    Thanks,
    -Oleg.

    Comment

    • Kevin Spencer

      #3
      Re: XML Serialization, collection with parental links

      Is there a better implementation?
      If not, is there a way to relink the parent elements manually upon
      deserialization ?
      Sure, just wire them back up after deserialization , using the parent.

      --
      HTH,

      Kevin Spencer
      Microsoft MVP
      Professional Chicken Magician

      A man, a plan, a canal.
      a palindrome that has gone to s**t.


      <Oleg.Ogurok@gm ail.comwrote in message
      news:1154292187 .918167.187680@ m73g2000cwd.goo glegroups.com.. .
      Hi there,
      >
      I'm writing a custom collection, which will be referened within a
      class. Each item within the collection needs to have a link to the
      parent container class, e.g.
      >
      Item x;
      Container c = x.ParentContain er;
      >
      I basically got the idea from ControlCollecti on class, where each
      control has a reference to its parent control.
      So far, I've implemented it as follows.
      >
      class Container
      {
      public ItemCollection Items { get; }
      }
      >
      class ItemCollection
      {
      public ItemCollection( Container container) { .. }
      public void Add(Item item) { _innerCollectio n.Add(item);
      item.SetContain er(_container); }
      }
      >
      class Item
      {
      // gets the reference to the parent container
      public Container ParentContainer { get; }
      }
      >
      I need to be able to serialize the Container class and all its members
      into XML, and then deserialize it. Upon deserialization , I'm able to
      get the values back into the collection. However, the links to the
      parent container are not preserved.
      I've read that .NET XML serializer doesn't work with circular
      references. Perhaps this could be the reason.
      >
      Is there a better implementation?
      If not, is there a way to relink the parent elements manually upon
      deserialization ?
      >
      Thanks,
      -Oleg.
      >

      Comment

      • Kevin Spencer

        #4
        Re: XML Serialization, collection with parental links

        Yes, that is what I was recommending as well.

        --
        HTH,

        Kevin Spencer
        Microsoft MVP
        Professional Chicken Magician

        A man, a plan, a canal.
        a palindrome that has gone to s**t.


        "simida" <youchangping@g mail.comwrote in message
        news:1154313133 .123459.80670@s 13g2000cwa.goog legroups.com...
        i don't how to relink the parent elements upon deserailization . but i
        think after deserailzation process , you can manually use this to
        relink relation.

        Class Continer
        {
        public void Relink()
        {
        foreach (Item item in this.Items)
        {
        item.SetContain er(this);
        }
        }
        }

        Sincerely,
        simida

        Oleg.Ogurok@gma il.com ??:
        Hi there,
        >
        I'm writing a custom collection, which will be referened within a
        class. Each item within the collection needs to have a link to the
        parent container class, e.g.
        >
        Item x;
        Container c = x.ParentContain er;
        >
        I basically got the idea from ControlCollecti on class, where each
        control has a reference to its parent control.
        So far, I've implemented it as follows.
        >
        class Container
        {
        public ItemCollection Items { get; }
        }
        >
        class ItemCollection
        {
        public ItemCollection( Container container) { .. }
        public void Add(Item item) { _innerCollectio n.Add(item);
        item.SetContain er(_container); }
        }
        >
        class Item
        {
        // gets the reference to the parent container
        public Container ParentContainer { get; }
        }
        >
        I need to be able to serialize the Container class and all its members
        into XML, and then deserialize it. Upon deserialization , I'm able to
        get the values back into the collection. However, the links to the
        parent container are not preserved.
        I've read that .NET XML serializer doesn't work with circular
        references. Perhaps this could be the reason.
        >
        Is there a better implementation?
        If not, is there a way to relink the parent elements manually upon
        deserialization ?
        >
        Thanks,
        -Oleg.

        Comment

        Working...