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

  • Home
  • SEARCH
  • 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 106847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:39:40+00:00 2026-05-11T01:39:40+00:00

When serializing, I would like to serialize an object only once, then any references

  • 0

When serializing, I would like to serialize an object only once, then any references to that object should be serialized as a reference to the object. This is because, when I later deserialize the objects, I would like to maintain those references. To illustrate my goal, the code below should output ‘After Serialization: true’.

class Program {     static void Main(string[] args)     {         MyObject obj = new MyObject() { Name = 'obj' };         MyObject[] myObjs = new MyObject[]         {             obj, obj         };          Console.WriteLine('Object Refs Equal?');         Console.WriteLine(string.Format('Before Serialization: {0}',             object.ReferenceEquals(myObjs[0], myObjs[1])));          XmlSerializer toXml = new XmlSerializer(typeof(MyObject[]));         using (FileStream toFile = File.Create(@'C:\foo.xml'))         {             toXml.Serialize(toFile, myObjs);         }          XmlSerializer fromXml = new XmlSerializer(typeof(MyObject[]));         using (FileStream fromFile = File.OpenRead(@'C:\foo.xml'))         {             MyObject[] deserialized = (MyObject[])fromXml.Deserialize(fromFile);              Console.WriteLine(string.Format('After Serialization: {0}',             object.ReferenceEquals(deserialized[0], deserialized[1])));         }          Console.ReadLine();     } }  [Serializable] public class MyObject {     public string Name { get; set; } } 
  • 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. 2026-05-11T01:39:41+00:00Added an answer on May 11, 2026 at 1:39 am

    If you’re using .NET 3 or later, you should use the DataContractSerializer and set PreserveObjectReferences to true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a method that accepts an object then attempts to serialize the
I'm de/serializing an object like so: public class myClass : ISerializable { public List<OType>
I'm looking for a way to serialize a POCO that contains some read-only properties.
I'm serializing a class like this public MyClass { public int? a { get;
I am using MappingJacksonHttpMessageConverter in Spring MVC to automatically serialize objects like this: @RequestMapping(value=/*/getAccount,
I'm serializing an object in a C# VS2003 / .Net 1.1 application. I need
I have a common code of serializing a class object in my 3-4 methods
I'm currently serializing an object using XMLSerializer, and the resulting XML starts with: <?xml
I'm working with a list of fonts that I serialize and deserialize using DataContractSerializer
I am receiving the following exception when trying to serialize an object using XMLSerialization.

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.