XmlSerialization is failing due to a circular reference.

J

Jeremy Kitchen

I have inherited a project and I am trying to figure out why the
failing units tests that exist fail. I a, getting the following error
when I attempt serialization.

Any advice on what I should do to find the source of the problem would
be appreciated.

Thank you
Jeremy

Error 1 TestCase
'SuccessAssessBusiness.SuccessAssessBusinessTest.LinkedAssessments'
failed: System.InvalidOperationException : There was an error
generating the XML document.
----> System.InvalidOperationException : A circular reference was
detected while serializing an object of type
SuccessAssessBusiness.ManagementGroup.
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces, String
encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces, String
encodingStyle)
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter
textWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter
textWriter, Object o)
at
SuccessAssessBusiness.SuccessAssessBusinessTest.LinkedAssessments() in
C:\Inetpub\wwwroot\SuccessAssess\Projects\SuccessAssess
\SuccessAssessTest\SuccessAssessBusinessTest.cs:line 372
--InvalidOperationException
at
System.Xml.Serialization.XmlSerializationWriter.WriteStartElement(String
name, String ns, Object o, Boolean writePrefixed,
XmlSerializerNamespaces xmlns)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAssessment.Write8_ManagementGroup(String
n, String ns, ManagementGroup o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAssessment.Write9_Jobsite(String
n, String ns, Jobsite o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAssessment.Write8_ManagementGroup(String
n, String ns, ManagementGroup o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAssessment.Write9_Jobsite(String
n, String ns, Jobsite o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAssessment.Write12_Assessment(String
n, String ns, Assessment o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAssessment.Write13_Assessment(Object
o) C:\Inetpub\wwwroot\SuccessAssess\Projects\SuccessAssess
\SuccessAssessTest\SuccessAssessBusinessTest.cs 372
 
N

Nicholas Paldino [.NET/C# MVP]

Jeremy,

XML serialization does not support circular references. This means that
one of the objects that you are trying to serialize holds onto another
object higher up in the object chain, which in turn causes your circular
reference. You need to make it so that the object does not reference itself
(directly or indirectly) and then the problem should go away.
 
M

Marc Gravell

A common gotcha when using xml serialization; if your objects have a
logical parent/child (tree) structure, then the simple fix is to apply
[XmlIgnore] to any "parent" relationships, so that it is serialized in
a downwards (only) route. Unfortunately, these relationships will need
repairing at t'other end. One solution here is custom serialization
(IXmlSerializable), but this is probably overkill. Maybe just fixup
manually after deserializing.

For reference, note that the DataContractSerializer (.Net 3.0) can (if
asked nicely) support both "graph" and "tree" serialization; but of
course the xml will look different...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top