Link to home
Start Free TrialLog in
Avatar of Misbah
MisbahFlag for United States of America

asked on

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 :(
[Serializable]
    public class SearchResult
    {
        private string searchQuery;
        [NonSerialized]
        private TimeSpan time;
        private int foundlHits;
        private int foundDocs;
        private int totalDocs;
        private bool hasErrors;
        private string errorMessage;
        [NonSerialized]
        private List<SearchResultEntry> entriesList;
        [NonSerialized]
        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();
 
        }
}
 
 
 [Serializable]
    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;
        [NonSerialized]
        private SearchResult parentResult;
        [NonSerialized]
        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);
            textWriter.Close();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of saragani
saragani

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Misbah

ASKER

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.

Thanks