Mike H.
Mike H.

Reputation: 1751

What exactly is a circular reference?

So I've dealt with circular reference exceptions with JSON serialization in the past and felt like I learned my painful lesson, but a new problem has arisen that makes me question whether or not I truly understand the problem I'm having to face yet again.

It is my understanding that a circular reference exception (in terms of JSON serializiation) is when either a child object references it's parent, or any object references itself. This is an understandable exception and removing these 'looping references' will alleviate the issue.

My problem is that I received a circular reference exception where one doesn't seem possible. I'm currently reading from an XML file into a list of it's elements, then serializing the results and passing it to an MVC view.

Here's the method:

    public JsonResult GetMakeModelData()
    {
        var path = Server.MapPath("/App_Data/XML/Make_Model_List.xml");
        var doc = XDocument.Load(path);
        var Makes = doc.Root.Elements().Select(x => x.Element("carname")).ToList();

        var jsonResult = Json(Makes, JsonRequestBehavior.AllowGet);
        jsonResult.MaxJsonLength = int.MaxValue;
        return jsonResult;
    }

...and the XML sample:

<carlist>
  <car>
    <carname>AC</carname>
    <carmodellist>
      <carmodel>Cobra</carmodel>
    </carmodellist>
  </car>
  <car>
    <carname>Acura</carname>
    <carmodellist>
      <carmodel>1.6 EL</carmodel>
      <carmodel>1.7 EL</carmodel>
      <carmodel>2.2 CL</carmodel>
      <carmodel>2.3 CL</carmodel>
      <carmodel>2.5 TL</carmodel>
      <carmodel>3.0 CL</carmodel>
      <carmodel>3.2 CL</carmodel>
      <carmodel>3.2 TL</carmodel>
      <carmodel>3.5 RL</carmodel>
      <carmodel>CL</carmodel>
      <carmodel>CSX</carmodel>
      <carmodel>EL</carmodel>
      <carmodel>Integra</carmodel>
      <carmodel>Legend</carmodel>
      <carmodel>MDX</carmodel>
      <carmodel>NSX</carmodel>
      <carmodel>NSX-T</carmodel>
      <carmodel>RDX</carmodel>
      <carmodel>RL</carmodel>
      <carmodel>RSX</carmodel>
      <carmodel>SLX</carmodel>
      <carmodel>TL</carmodel>
      <carmodel>TSX</carmodel>
      <carmodel>Vigor</carmodel>
    </carmodellist>
  </car>
  ....
</carlist>

Now as you would expect, I get a list of 97 elements returned from the LINQ query. When passing to the view, I get a circular reference exception...on an object, that contains nothing more than 97 unique strings. Where is the exception coming from and why is it happening?!

Upvotes: 1

Views: 192

Answers (1)

Eren Ers&#246;nmez
Eren Ers&#246;nmez

Reputation: 39085

Possibly, your problem is that you are trying to convert a List<XElement> to json, not a List<string>. Each XElement has a reference to its parent element, which has a reference back to its children, so you get circular references.

Try this instead, and see if it helps:

x.Element("carname").Value //get the string value instead of the XElement itself

Upvotes: 3

Related Questions