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 3212216
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:48:55+00:00 2026-05-17T14:48:55+00:00

I am receiving the following exception when trying to serialize an object using XMLSerialization.

  • 0

I am receiving the following exception when trying to serialize an object using XMLSerialization.

A circular reference was detected while serializing an object of type MyObject}

I know the circular reference is because ObjectA can have a childObject of ObjectB and ObjectB’s parentObject is ObjectA, however I would like to keep that reference if possible . Is there a way to get this object to serialize with XML Serialization without losing any data during the serialization process? I’m not very familar with serialization so I’m hoping theres some kind of Attribute I could 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. Editorial Team
    Editorial Team
    2026-05-17T14:48:56+00:00Added an answer on May 17, 2026 at 2:48 pm

    There are several options depending on serializer type.

    If you could use DataContractSerializer or BinaryFormatter then you may use OnSerializedAttribute and set Parent property for your child object to this:

    [Serializable]
    public class Child
    {
        public string Foo { get; set; }
    
        public Parent Parent { get { return parent; } set { parent = value; } }
    
        // We don't want to serialize this property explicitly.
        // But we could set it during parent deserialization
        [NonSerialized]
        private Parent parent;
    }
    
    [Serializable]
    public class Parent
    {
        // BinaryFormatter or DataContractSerializer whould call this method
        // during deserialization
        [OnDeserialized()]
        internal void OnSerializedMethod(StreamingContext context)
        {
            // Setting this as parent property for Child object
            Child.Parent = this;
        }
    
        public string Boo { get; set; }
    
        public Child Child { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Child c = new Child { Foo = "Foo" };
            Parent p = new Parent { Boo = "Boo", Child = c };
    
            using (var stream1 = new MemoryStream())
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof (Parent));
                serializer.WriteObject(stream1, p);
                stream1.Position = 0;
                var p2 = (Parent)serializer.ReadObject(stream1);
    
                Console.WriteLine(object.ReferenceEquals(p, p2)); //return false
                Console.WriteLine(p2.Boo); //Prints "Boo"
    
                //Prints: Is Parent not null: True
                Console.WriteLine("Is Parent not null: {0}", p2.Child.Parent != null);
            }
        }
    
    }
    

    If you want to use XmlSerializer you should implement IXmlSerializable, use XmlIgnoreAttribute and implemented more or less the same logic in ReadXml method. But in this case you should also implement all Xml serialization logic manually:

    [Serializable]
    public class Child
    {
        public Child()
        {
        }
    
        public string Foo { get; set; }
    
        [XmlIgnore]
        public Parent Parent { get; set; }
    }
    
    [Serializable]
    public class Parent
    {
        public Parent()
        {
        }
    
        #region IXmlSerializable Members
    
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }
    
        public void ReadXml(System.Xml.XmlReader reader)
        {
            //Reading Parent content
            //Reading Child
            Child.Parent = this;
        }
    
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            //Writing Parent and Child content
        }
    
        #endregion
    
        public string Boo { get; set; }
    
        public Child Child { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am receiving the following exception while trying to make an SSL connection to
I am receiving the following exception when trying to run my unit tests using
I am receiving the following exception trying to browse a medium trust ASP.NET MVC
Background I'm receiving the following error when trying to render a partial view in
While trying to transform the date format I get an exception:unparseable date and don't
I am receiving this error when trying to execute applescript from my java application.
I am trying to redirect the Console output of a C#/Mono application into a
I've been having this issue for a while now and was never really able
I am attempting to retrieve a list of objects from Entity Framework via WCF,
Today, I removed and reinstalled the latest version of lampp in order to move

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.