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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:01:25+00:00 2026-05-14T02:01:25+00:00

I imagine to use XML serialization like this: class Foo { public Foo (string

  • 0

I imagine to use XML serialization like this:

class Foo {
    public Foo (string name) {
        Name1 = name;
        Name2 = name;
    }

    [XmlInclude]
    public string Name1 { get; private set; }

    [XmlInclude]
    private string Name2;
}

StreamWriter wr = new StreamWriter("path.xml");
new XmlSerializer<Foo>().Serialize (wr, new Foo ("me"));

Edit: I know this code is wrong. It was just to display how I would like to use it.

But this does not work at all:

  • XmlSerializer is not generic. I have to cast from and to object on (de)serialization.
  • Every property has to be fully public. Why aren’t we just using Reflection to access private setters?
  • Private fields cannot be serialized. I’d like to decorate private fields with an attribute to have XmlSerializer include them.

Did I miss something and XmlSerializer is actually offering the described possibilities? Are there alternate serializers to XML that handle these cases more sophisticatedly?

If not: We’re in 2010 after all, and .NET has been around for many years. XML serialization is often used, totally standard and should be really easy to perform. Or is my understanding possibly wrong and XML serialization ought not to expose the described features for a good reason?

Edit: Legacy is not a good reason imo. Listwas nongeneric at first, too.

(Feel free to adjust caption or tags. If this should be CW, please just drop a note.)

  • 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-14T02:01:26+00:00Added an answer on May 14, 2026 at 2:01 am

    First the fixed code, then the answers to your questions:

    public class Foo {
        public Foo() : this("") {}
        public Foo (string name) {
            Name1 = name;
            Name2 = name;
        }
        // note only this will be serialized
        public string Name1 { get; private set; }
        // this won't
        private string Name2;
    }
    

    or in 3.0:

    [DataContract]
    class Foo {
        public Foo (string name) {
            Name1 = name;
            Name2 = name;
        }
        [DataMember]
        public string Name1 { get; private set; }
        [DataMember]
        private string Name2;
    }
    

    (and use DataContractSerializer instead of XmlSerializer)

    XmlSerializer is not generic. I have to cast from and to object on (de)serialization.

    That is common for serializers. I have my own serializer, and initially I did make it fully generic. And it turned out to be a big design mistake. Huge. No, seriously. I’m currently in the process of re-writing every line of code to switch it out.

    Simply; serializers generally involve some level of reflection (either for code-gen or for the actual work, depending on the implementation). Reflection and generics don’t play nicely, especially on some of the frameworks like WCF. Having your code do the final cast is a fair compromise. I have a number of blog entries on this if you really want…

    Every property has to be fully public.

    That is indeed a limitation of XmlSerializer (although a list/colletion without a setter is fine, it will throw if you have a public get and private set). Also, the type needs to be public and have a parameterless constructor.

    Why aren’t we just using Reflection to access private setters?

    For performance, XmlSerializer builds an assembly on the fly to do what you want. It doesn’t have automatic access to your code’s internals. For info, I’m doing something similar but I offer 2 levels of generation; fully static (into a deployable dll), which then only works with public members, or in-memory, which can still access private members. I guess they wanted to settle on only 1 model, which makes sense – and they needed “sgen”, which dictates the first model.

    Private fields cannot be serialized. I’d like to decorate private fields with an attribute to have XmlSerializer include them.

    Then use DataContractSerializer, which will serialize any member (including private) marked [DataMember].

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

Sidebar

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This one is updated after each release: http://cksource.com/forums/viewtopic.php?f=11&t=15882 May 16, 2026 at 4:51 pm
  • Editorial Team
    Editorial Team added an answer If you want to get the values back into the… May 16, 2026 at 4:51 pm
  • Editorial Team
    Editorial Team added an answer If you want to use jetty maven plugin <plugin> <groupId>org.mortbay.jetty</groupId>… May 16, 2026 at 4:51 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Imagine the following XML document: <root> <person_data> <person> <name>John</name> <age>35</age> </person> <person> <name>Jim</name> <age>50</age>
Imagine the UI passes back an XMl node as such: <properties> <type> Source </type>
Imagine you have a library for working with some sort of XML file or
Imagine I create an instance of Foo on the heap in a method/function and
Imagine that I have a general class Person . Then I have specializations of
I'd like to use Maven to include all the dependencies needed to run any
Let's imagine that we have something like: $.post('somescript.php', 'WHAT CAN WE PUT HERE?', function(replyData)
Imagine I've got a data object that makes sense in an OO model, but
I'm trying to use the entity framework to take data (nested relationships) from one
I would like any advice on how to create and visualize a link map

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.