What's the simplest way to get XmlSerializer to also serialize private and "public const" properties of a class or struct? Right not all it will output for me is things that are only public. Making it private or adding const is causing the values to not be serialized.
-
2Why you want constants to be serialized?! You seem to be going a wrong route. – Mehrdad Afshari Jan 09 '09 at 19:36
-
By definition, exposed (i.e. public & protected) constants are something that isn't ever going to change. Changing a public constant is a client breaking change! For something that is merely constant for the run of the application, or for a particular version of the application, but may change in the future, you need to use `readonly` fields. `XmlSerializer` won't handle them either, but it's a different matter. – Pavel Minaev Jul 28 '09 at 21:51
-
If you want to serialize/deserialize private members please see http://stackoverflow.com/questions/420662/can-an-internal-setter-of-a-property-be-serialized – Andrew Hare Jan 09 '09 at 19:45
6 Answers
XmlSerializer
only looks at public fields and properties. If you need more control, you can implement IXmlSerializable and serialize whatever you would like. Of course, serializing a constant doesn't make much sense since you can't deserialize to a constant.

- 175,602
- 35
- 392
- 393
Even though it's not possible to serialize private properties, you can serialize properties with an internal setter, like this one :
public string Foo { get; internal set; }
To do that, you need to pre-generate the serialization assembly with sgen.exe, and declare this assembly as friend :
[assembly:InternalsVisibleTo("MyAssembly.XmlSerializers")]

- 286,951
- 70
- 623
- 758
Check out DataContractSerializer, introduced in .NET 3.0. It also uses XML format, and in many ways, it is better than XmlSerializer, including dealing with private data. See http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/ for a full comparison.
If you only have .NET 2.0, there's the BinarySerializer that can deal with private data, but of course it's a binary format.

- 1,214
- 13
- 24
-
1'Better' is debatable, for instance, the DataContractSerializer doesn't give you any flexibility on how the object is serialized. – nicodemus13 Mar 14 '12 at 10:23
It doesn't make sense to consider const
members, as they aren't per-instance; but if you just mean non-public instance members: consider DataContractSerializer
(.NET 3.0) - this is similar to XmlSerializer
, but can serialize non-public properties (although it is "opt in").
See here for more.

- 1
- 1

- 1,026,079
- 266
- 2,566
- 2,900
-
2Serializing the value of a constant for a particular runtime makes plenty of sense, if for example it should be compared against the value in another runtime. – Grault Dec 15 '14 at 20:43
-
@Jesdisciple not really - because again: this is an object (instance) serializer - and that value doesn't belong to the object (instance). – Marc Gravell Dec 16 '14 at 08:09
One other solution the use of Newtonsoft.Json:
var json = Newtonsoft.Json.JsonConvert.SerializeObject(new { root = result });
var xml = (XmlDocument)Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json);
Sure, this one has unfortunately detour via json.

- 122
- 1
- 1
- 8
Here's my solution to putting immutable values in a property that will serialize to XML:
[XmlElement]
public string format { get { return "Acme Order Detail XML v3.4.5"; } set { } }

- 535
- 4
- 13