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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:56:52+00:00 2026-06-14T06:56:52+00:00

I am using Data Contract serialization to serialize the following classes in XML: [DataContract]

  • 0

I am using Data Contract serialization to serialize the following classes in XML:

[DataContract]
public partial class Foo
{
    [DataMember]
    public string MyString { get; set; }
    [DataMember]
    public int MyInt { get; set; }
    [DataMember]
    public Bar MyBar { get; set; }
}

[DataContract]
public class Bar
{
    public int BarId { get; set; }
    [DataMember]
    public string BarField { get; set; }
}

When I serialize it it generates XML like this:

<Foo>
    <MyString>My text</MyString>
    <MyInt>2</MyInt>
    <MyBar>
        <BarField>My bar field</BarField>
    </MyBar>
</Foo>

What I would like to do is have the MyBar field not appear as a complex type, but like this instead:

<Foo>
    <MyString>My text</MyString>
    <MyInt>2</MyInt>
    <MyBar>My bar field</MyBar>
</Foo>

I’m new to data contract serialization and haven’t found any kind of tutorials that address my issue. I don’t even know if it is possible or not but I figured I’d ask before I gave up and dealt with it the way it is or found a better solution.

  • 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-06-14T06:56:54+00:00Added an answer on June 14, 2026 at 6:56 am

    By decorating data classes as DataContract, you are inherently setting the structure (Xml, Json etc) that the data will be represented as when it is serialized.

    Can I suggest that you split the concerns of ‘Business Entity’ and ‘Serialization Entity’ out of your data classes, e.g. if Foo + Bar are your in memory or ORM representations of the data, then remove the [DataContract]s from them:

    public partial class Foo
    {
        public string MyString { get; set; }
        public int MyInt { get; set; }
        public Bar MyBar { get; set; }
    }
    
    public class Bar
    {
        public string BarField { get; set; }
    }
    

    Then, provide a new class specifically for the serialized format, decorated with DataContract

    [DataContract]
    public partial class SerializableFoo
    {
        [DataMember]
        public string MyString { get; set; }
        [DataMember]
        public int MyInt { get; set; }
        [DataMember]
        public string MyBar { get; set; }
    }
    

    And then provide a mapping function to map from the one to the other. LINQ is awesome for this kind of work, and if the classes have mostly the same property names, then AutoMapper can do a lot of the work for you

    e.g.

    var wireFoo = new SerializableFoo()
    {
        MyString = foo.MyString,
        MyInt = foo.MyInt,
        MyBar = foo.Bar.BarField // Do the projection / flattening here
    }
    // Serialize wireFoo here, or return it from a WCF Service, etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code, [DataContract] public class TestClass { [DataMember] public object _TestVariable; public
I'm using the System.Runtime.Serialization.Json library This doesn't Work public class Detections { [DataContract] public
I'm trying to serialize a class derived from List<> using DataContract. The problem is
If i try to serialize an object of the following ClassToSerialize class with System.Runtime.Serialization.Json.DataContractJsonSerializer
I have a class structure in C#, similar to the following: [DataContract] class Data
How do I serialize a list without the outer element using the Data Contract
I have these classes: [DataContract] public class ErrorBase {} [DataContract] public class FileMissingError: ErrorBase
I am trying to serialize a hash table using the link XML serialization of
I am using a data contract in WCF, but there is some unwanted response
I've been using WCF's DataContract and DataContractSerializer to read/write objects to XML files. We

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.