Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6902261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:46:35+00:00 2026-05-27T07:46:35+00:00

I have a 2 collection of varying objects. Let’s say I have the following

  • 0

I have a 2 collection of varying objects.

Let’s say I have the following two objects

Private col1 as Collection(Of A) and Private col2 as Collection(Of B)

But object of Type A has a collection of Type B as an Attribute.

so A looks like that

Public Class A
    Public Property myStringProp() as string
    Public Property colB() as Collection(Of B)
End Class

whereas B looks like

Public Class B
    Public Property myStringProp() as string
End Class

So in col2 i can have e.g. 20 items of Type B.
In col1 I have e.g. 2 items of Type A. Each of them has n references to items of Type B to the collection of col2.

How can I serialize and deserialize these Objects so that the references will be restored when deserializing?

Preferred Serialization with XML.

I have tried to use the DataContractSerializer but I have no idea where and how to use it.

Edit:

OK. I would be able to resolve them manually. But I don’t like the way:

  For Each itema As A In col1
     For Each itemb As B In itema.colB
        For Each objB In col2
           If itemb.myStringProp = objB.myStringProp Then
              itemb = objB
           End If
        Next
     Next
  Next

This would just loop through all objects of A in col1 and then loop through all objects of B and search an object in col2 with the same value for myStringProp.

So any cleaner solution would be appreciated 🙂

So any cleaner solutions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:46:36+00:00Added an answer on May 27, 2026 at 7:46 am

    The serializer can preserve object references in a single serialization episode. So if you have both collections as members of a single object (which is then serialized / deserialized), you can use the preserveObjectReferences parameter in the DataContractSerializer constructor, and you’ll get that. Another option is to decorate the type with <DataContract(IsReference:=True)>, which can also be used to preserve the references. The code below shows the first approach.

    Public Class StackOverflow_8387789
        Public Class A
            Public Property myStringProp() As String
            Public Property colB() As Collection(Of B)
        End Class
    
        Public Class B
            Public Property myStringProp() As String
        End Class
    
        Public Class Both
            Public Property col1 As Collection(Of A)
            Public Property col2 As Collection(Of B)
        End Class
    
        Public Shared Sub Test()
            Dim both = New Both()
            both.col2 = New Collection(Of B)
            both.col2.Add(New B With {.myStringProp = "B1"})
            both.col2.Add(New B With {.myStringProp = "B2"})
            both.col2.Add(New B With {.myStringProp = "B3"})
            both.col1 = New Collection(Of A)
            Dim colBForA1 = New Collection(Of B)
            colBForA1.Add(both.col2(0))
            colBForA1.Add(both.col2(1))
            Dim colBForA2 = New Collection(Of B)
            colBForA2.Add(both.col2(1))
            colBForA2.Add(both.col2(2))
            both.col1.Add(New A With {.myStringProp = "A1", .colB = colBForA1})
            both.col1.Add(New A With {.myStringProp = "A2", .colB = colBForA2})
            Dim dcs = New DataContractSerializer(GetType(Both), Nothing, Integer.MaxValue, False, True, Nothing)
            Dim ms = New MemoryStream()
            Dim ws = New XmlWriterSettings With { _
                    .Encoding = Encoding.UTF8,
                    .Indent = True,
                    .IndentChars = "  ",
                    .OmitXmlDeclaration = True
                }
            Dim xw = XmlWriter.Create(ms, ws)
            dcs.WriteObject(xw, both)
            xw.Flush()
            Console.WriteLine("Serialized: {0}", Text.Encoding.UTF8.GetString(ms.ToArray()))
    
            ms.Position = 0
            Console.WriteLine("Now deserializing:")
            Dim both2 = CType(dcs.ReadObject(ms), Both)
            Console.WriteLine("Is both.col1(0).colB(0) = both.col2(0)? {0}", both2.col1(0).colB(0) Is both2.col2(0))
            Console.WriteLine("Is both.col1(1).colB(1) = both.col2(2)? {0}", both2.col1(1).colB(1) Is both2.col2(2))
            Console.WriteLine("Is both.col1(0).colB(0) = both.col2(2) (should be False)? {0}", both2.col1(0).colB(0) Is both2.col2(2))
        End Sub
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have collection of documents with specified longitude and latitude. type is
I have Class and Student objects. Both have collection of another as property. Which
We have a collection of entities of type called Unit , collection of entities
I have collection of maps. Collection<Map<String,Object>> xyz = (Collection<Map<String,Object>>) someMethod(); xyz.add(new HashMap<>()); If I
I have collection of object as Collection basics = periodic.getGeneratedBasic(); When iterate over this
I have a collection of fixed width files with a varying number of columns
I have collection of multidimensional object (e.g class Person = {age : int ,
I have collection of inputs of type checkbox var collection = $('.className'); console.log(collection); result
I have look all over the net, and I find varying answers, but had
I have collection of objects. I need to sort it by difficult condition, using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.