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 195365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:40:29+00:00 2026-05-11T16:40:29+00:00

I’m in the process of writing a WCF application which will be consumed by

  • 0

I’m in the process of writing a WCF application which will be consumed by a Silverlight application. I have done most of the design work and I am now doing the implementation, which has made me come up with this question.

Here’s an example of something that exists in my application:

[DataContract]
class Person
{
    [DataMember]
    private Towel mostRecentlyUsedTowel;

    [DataMember]
    private Gym gym; //the gym that this person attends

    ...
}

[DataContract]
class Gym
{
    [DataMember]
    private List<Towel> towels; //all the towels this gym owns

    ...
}

Here’s what I’m getting at: In my application mostRecentlyUsedTowel will be pointing at something in the towels list for the person’s gym. Some of my requests will serialize a Person object.

Is the DataContractSerializer smart enough to notice its being asked to serialize the exact same instance of an object twice? If so, how does it deal with it?

If it will just go about serializing the same instance twice, how should I deal with this so I’m not sending unnecessary data over the link?

  • 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-11T16:40:29+00:00Added an answer on May 11, 2026 at 4:40 pm

    The following code:

    [TestMethod]
    public void CanSerializePerson()
    {
        var towel1 = new Towel() { Id = 1 };
        var towel2 = new Towel() { Id = 2 };
        var towel3 = new Towel() { Id = 3 };
        var gym = new Gym();
        gym.towels.Add(towel1);
        gym.towels.Add(towel2);
        gym.towels.Add(towel3);
    
        var person = new Person()
        {
          recentlyUsedTowel = towel1,
          gym = gym
        };
    
        var sb = new StringBuilder();
        using (var writer = XmlWriter.Create(sb))
        {
            var ser = new DataContractSerializer(typeof (Person));
            ser.WriteObject(writer, person);
        }
    
        throw new Exception(sb.ToString());
    }
    
    public class Person
    {
        public Towel recentlyUsedTowel { get; set; }
        public Gym gym { get; set; }
    }
    
    public class Gym
    {
        public Gym()
        {
            towels = new List<Towel>();
        }
    
        public List<Towel> towels { get; set; }
    }
    
    
    public class Towel
    {
        public int Id { get; set; }
    }
    

    will evaluate to:

    <?xml version="1.0" encoding="utf-16"?>
    <Person xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org">
      <gym>
        <towels>
          <Towel><Id>1</Id></Towel>
          <Towel><Id>2</Id></Towel>
          <Towel><Id>3</Id></Towel>
        </towels>
      </gym>
      <recentlyUsedTowel><Id>1</Id></recentlyUsedTowel>
    </Person>
    

    If you added the IsReference property to the DataContract attribute of the Towel class like this:

    [DataContract(IsReference=true)]
    public class Towel
    {
        // you have to specify a [DataMember] in this because you are 
        // explicitly adding DataContract
        [DataMember]
        public int Id { get; set; }
    }
    

    you would get an output like this:

    <?xml version="1.0" encoding="utf-16"?>
    <Person xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org">
      <gym>
        <towels>
          <Towel z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
            <Id>1</Id>
          </Towel>
          <Towel z:Id="i2" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
            <Id>2</Id>
          </Towel>
          <Towel z:Id="i3" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
            <Id>3</Id>
          </Towel>
        </towels>
      </gym>
      <recentlyUsedTowel z:Ref="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" />
    </Person>
    

    Hope this Helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
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
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.