XML Serialization

Hi Experts,

I need your help!
I am getting an exception while trying to serialize a custom object I wrote.
Exception:System.InvalidOperationException: A circular reference was detected while serializing an object of type SmartObjects.SearchResult.

I added the  [NonSerialized] attribute to some fields to avoid this but still no luck.
sorry I am running out of points :(
    public class SearchResult
        private string searchQuery;
        private TimeSpan time;
        private int foundlHits;
        private int foundDocs;
        private int totalDocs;
        private bool hasErrors;
        private string errorMessage;
        private List<SearchResultEntry> entriesList;
        private SearchNode searchNode;
        // I removed the properties and other methods to make the code easier to read
        public SearchResult()
            searchQuery = "";
            time =  new TimeSpan();
            totalDocs = 0;
            foundDocs = 0;
            foundlHits = 0;
            entriesList = new List<SearchResultEntry>();
            searchNode = new SearchNode();
    public class SearchResultEntry
        private string docPath;
        private int docID;
        private int hitCount;
        private int size;
        private string type;
        private DateTime modified;
        private string dataType;
        private string longname;
        private string programName;
        private SearchResult parentResult;
        private SearchNode searchNode;
        public SearchResultEntry()
  SearchResult rs = new SearchResult();
  // add entries to rs and then try to serialize the object
  XmlSerializer serializer = new XmlSerializer(typeof(SearchResult));
            TextWriter textWriter = new StreamWriter(@"C:\file.xml");
            serializer.Serialize(textWriter, rs);

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You have removed some properties and I'm guessing that the code that you gave here will actually get serialized.

Let me try guessing that SearchResultEntry that contains:
private SearchResult parentResult;

also contains a public property that reveals that Parent Result. This causes a circular reference.

Put [XmlIgnore] attribute above the public property of the Parent Result.

Btw, only public properties and objects get serialized on XML serialization and the [XmlIgnore] should be put on those publics that you don't wanna serialize (not the pro=ivates).
I'm not familiar with the [NonSerialized] attribute so I don't know if it does the same as XmlIgnore.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MisbahAuthor Commented:
I did some workaround and I don't need to do the serialization but your solution make sense and I will just assign the points and close the question without verifying the answer.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.