Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:57:33+00:00 2026-05-27T04:57:33+00:00

I’m building an XNA game and I’m trying to save game/map etc. state completely,

  • 0

I’m building an XNA game and I’m trying to save game/map etc. state completely, and then be able to load and resume from exactly the same state.

My game logic consists of fairly complex elements (for serializing) such as references, delegates etc. I’ve done hours of research and decided that it’s the best to use a DataContractSerializer that preserves the object references. (I also got around for delegates but that’s another topic) I have no problem serializing and deserializing the state, re-creating the objects, the fields, lists, and even object references correctly and completely. But I’ve got a problem with cyclic references. Consider this scenario:

class X{
   public X another;
}

//from code:
X first = new X();
X second = new X();
first.another = second;
second.another = first;

Trying to serialize X will result in an exception complaining about cyclic references. If I comment out the last line it works fine. Well, I can imagine WHY it is happening, but I have no idea HOW to solve it. I’ve read somewhere that I can use the DataContract attribute with IsReference set to true, but it didn’t change anything for me — still got the error. (I want to avoid it anyway since the code I’m working on is portable code and may someday run on Xbox too, and portable library for Xbox doesn’t support the assembly that DataContract is in.)

Here is the code to serialize:

class DataContractContentWriterBase<T> where T : GameObject
{
    internal void Write(Stream output, T objectToWrite, Type[] extraTypes = null)
    {
        if (extraTypes == null) { extraTypes = new Type[0]; }
        DataContractSerializer serializer = new DataContractSerializer(typeof(T), extraTypes, int.MaxValue, false, true, null);
        serializer.WriteObject(output, objectToWrite);
    }
}

and I’m calling this code from this class:

[ContentTypeWriter]
public class PlatformObjectTemplateWriter : ContentTypeWriter<TWrite>
(... lots of code ...)
    DataContractContentWriterBase<TWrite> writer = new DataContractContentWriterBase<TWrite>();
    protected override void Write(ContentWriter output, TWrite value)
    {
        writer.Write(output.BaseStream, value, GetExtraTypes());
    }

and for deserialization:

class DataContractContentReaderBase<T> where T: GameObject
{
    internal T Read(Stream input, Type[] extraTypes = null)
    {
        if (extraTypes == null) { extraTypes = new Type[0]; }
        DataContractSerializer serializer = new DataContractSerializer(typeof(T), extraTypes, int.MaxValue, false, true, null);
        T obj = serializer.ReadObject(input) as T;
        //return obj.Clone() as T; //clone falan.. bi bak iste.
        return obj;
    }
}

and it’s being called by:

public class PlatformObjectTemplateReader : ContentTypeReader<TRead>
(lots of code...)
    DataContractContentReaderBase<TRead> reader = new DataContractContentReaderBase<TRead>();
    protected override TRead Read(ContentReader input, TRead existingInstance)
    {
        return reader.Read(input.BaseStream, GetExtraTypes());
    }

where:

PlatformObjectTemplate was my type to write.

Any suggestions?

SOLUTION: Just a few minutes ago, I’ve realized that I wasn’t marking the fields with DataMember attribute, and before I added the DataContract attribute, the XNA serializer was somehow acting as the “default” serializer. Now, I’ve marked all the objects, and things are working perfectly now. I now have cyclic references with no problem in my model.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:57:33+00:00Added an answer on May 27, 2026 at 4:57 am

    If you don’t want to use [DataContract(IsReference=true)] then DataContractSerializer won’t help you, because this attribute is the thing that does the trick with references.

    So, you should either look for alternative serializers, or write some serialization code that transforms your graphs into some conventional representation (like a list of nodes + a list of links between them) and back, and then serialize that simple structure.

    In case you decide to use DataContract(IsReference=true), here’s a sample that serializes your graph:

    [DataContract(IsReference = true)]
    class X{
      [DataMember]
      public X another;
    }
    
    static void Main()
    {
      //from code:
      var first = new X();
      var second = new X();
      first.another = second;
      second.another = first;
    
      byte[] data;
      using (var stream = new MemoryStream())
      {
        var serializer = new DataContractSerializer(typeof(X));
    
        serializer.WriteObject(stream, first);
        data = stream.ToArray();
      }
      var str = Encoding.UTF8.GetString(data2);
    }
    

    The str will contain the following XML:

    <X z:Id="i1" xmlns="http://schemas.datacontract.org/2004/07/GraphXmlSerialization"
       xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
      <another z:Id="i2">
        <another z:Ref="i1"/>
      </another>
    </X>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.