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

  • Home
  • SEARCH
  • 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 1025661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:57:40+00:00 2026-05-16T11:57:40+00:00

I’ve got a class that when serialized to XML looks like this (generalized for

  • 0

I’ve got a class that when serialized to XML looks like this (generalized for simplicity):

<root>
  <resources>
    <resource name="foo" anotherattribute="value">data</resource>
    <resource name="bar" anotherattribute="value">more data</resource>
  </resource>
  <myobject name="objName">
    <resource name="foo" />
  </myobject>
</root>

When it’s deserialized, I need the instance of resource referenced in the property of the myobject instance to be the same object created during the deserialization of the resources collection. Also, if possible I don’t want to have to output the full serialization of the resource instance in myobject, only the name.

Is there any way of doing this? Right now I’m considering resorting to having a separate string property for serialization purposes that gets the relevant object from root when the deserializer sets the property, but that means giving myobject a reference to the root that contains it, and I was hoping to avoid that coupling.

  • 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-16T11:57:41+00:00Added an answer on May 16, 2026 at 11:57 am

    You can’t do that with XmlSerializer, because it doesn’t handle object references.

    If you don’t have any constraint on the generated schema, you could use DataContractSerializer, which also serializes to XML but supports references. To use DataContractSerializer, each type must have a DataContract attribute, and each member you want to serialize must have a DataMember attribute :

    [DataContract(Name = "root")]
    public class root
    {
        [DataMember]
        public List<resource> resources { get; set; }
        [DataMember]
        public myobject myobject { get; set; }
    }
    
    [DataContract]
    public class myobject
    {
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public resource resource { get; set; }
    }
    
    [DataContract(Name = "resource", IsReference = true)]
    public class resource
    {
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public string anotherattribute { get; set; }
        [DataMember]
        public string content { get; set; }
    }
    
    ...
    
    var serializer = new DataContractSerializer(typeof(root));
    using (var xwriter = XmlWriter.Create(fileName))
    {
        serializer.WriteObject(xwriter, r);
    }
    

    Note the IsReference = true for the resource class: that’s what makes the serializer handle this class by reference. In the generated XML, each resource instance is only serialized once:

    <?xml version="1.0" encoding="utf-8"?>
    <root xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/">
      <myobject>
        <name>objName</name>
        <resource z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
          <anotherattribute>value</anotherattribute>
          <content>data</content>
          <name>foo</name>
        </resource>
      </myobject>
      <resources>
        <resource z:Ref="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" />
        <resource z:Id="i2" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
          <anotherattribute>value</anotherattribute>
          <content>more data</content>
          <name>bar</name>
        </resource>
      </resources>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I know there's a lot of other questions out there that deal with this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.