Misbah
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.InvalidOp erationExc eption: 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 :(
I need your help!
I am getting an exception while trying to serialize a custom object I wrote.
Exception:System.InvalidOp
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();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks