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?
saraganiConnect With a Mentor Commented:
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.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.