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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:01:20+00:00 2026-05-27T03:01:20+00:00

Am I correct in thinking that, in order to get C# to serialize an

  • 0

Am I correct in thinking that, in order to get C# to serialize an object, I MUST have a public property for every field that needs its state stored?

If so, is that not very very sucky, as it weakens (if not breaks entirely) any encapsulation my class has?

In Java, XStream can iterate over every non-transient field and archive it. In C# this can’t happen, and just to make things worse, things like Dictionaries don’t serialize AT ALL. It’s all a bit of a mess, no?

I’ve seen the DLL for a “port” of XStream to .net, but there are no docs and I’m suspicious.

  • 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-27T03:01:21+00:00Added an answer on May 27, 2026 at 3:01 am

    You should use DataContractSerializer, and mark every property/field you want to serialize with [DataMember]. It doesn’t care if your fields are private or public. By the way you can serialize Dictionaries with it…

    [DataContract]
    public class MyClass
    {
        [DataMember]
        private string _privateField;
    
        [DataMember]
        public int PublicProperty { get; set;}
    }
    

    Serialization :

    private static string SerializeXml<T>(T item)
    {
        DataContractSerializer ser = new DataContractSerializer(item.GetType());
    
        StringBuilder sb = new StringBuilder();
        XmlWriterSettings settings = new XmlWriterSettings { OmitXmlDeclaration = true, ConformanceLevel = ConformanceLevel.Fragment };
        using (XmlWriter writer = new XmlWriter(sb, settings))
        {
            ser.WriteObject(writer, item);
        }
        return sb.ToString();
    }
    

    Look here for differences between XmlSerializer and DataContractSerializer : http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/

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

Sidebar

Related Questions

Am I correct in thinking that that Python doesn't have a direct equivalent for
Am I correct in thinking that if I have a WCF OperationContract takes in
I have a repository: public ObservableCollection<ProjectExpenseBO> GetProjectExpenses() { //Get by query IQueryable<ProjectExpenseBO> projectExpenseQuery =
I have an Order object to represent a Prospective Order/Receipt. This is an entity.
I'm thinking of creating a simple game in order to help get my head
I have an application and in order to get the images to load into
I have a daemon that reads a configuration file in order to know where
Let's say I have a theoretical MVC framework that uses a ViewData object to
Correct me if I'm wrong, but a build is a compile, and not every
Am I correct in assuming that the only difference between "windows files" and "unix

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.