116

I have a serializable class and one of the properties in my class generates a Guid in the getter. The property implements no setter and is ignores during serialization. Why is that and do I always have to implement a setter in order for my property to be serialized.

[Serializable]
public class Example
{
    [XmlAttribute("id")]
    public string Id
    {
        get
        {
             return Guid.NewGuid().ToString();
        }
    }
}

I tried implementing an empty setter and it got serialized correctly.

[Serializable]
public class Example
{
    [XmlAttribute("id")]
    public string Id
    {
        get
        {
             return Guid.NewGuid().ToString();
        }
        set {}
    }
}

Update:

Can you point out how should I define properties whose values never change or ones that the value for is generated internally?

4

5 Answers 5

65

It's a limitation of XmlSerializer it doesn't serialize read-only properties, what you have done in your second example is essentially the hack to get it to serialize, however, it's useless if you need it to deserialize later.

Alternatively you could switch to using DataContractSerializer, it's more flexible.

9
  • 8
    DataContractSerializer also requires setters. This is a limitation because even though we may use it for only one type of operation, say deserialization, but the declarations need to be for both otherwise, the serialization process will not know what to do with a value when it is trying to serialize an XML into objects.
    – Ravi Y
    Nov 15, 2012 at 15:55
  • 7
    @ryadavilli anything marked as [DataMember] is serialized using DataContractSerializer (read-only or not).
    – James
    Nov 15, 2012 at 15:59
  • 1
    @James I wrote the above because i got some compile time errors when I tried the same. Now, when I got back to my code and removed the setter, it seems to compiling fine. This is very wierd and probably homework for me to research more on this.
    – Ravi Y
    Nov 15, 2012 at 16:13
  • 3
    Ahhh finally found the source of my confusion. A property setter is not required for serialization. However, if a property does not have a setter, the XML will not deserialize to an object. There, homework finished.
    – Ravi Y
    Nov 15, 2012 at 16:24
  • 1
    @ryadavilli yep exactly, it's just a limitation of the XmlSerializer implementation, it relies on the public setter method. DataContractSerializer is a bit more clever in that sense (probably using reflection to set the field behind the scenes).
    – James
    Nov 15, 2012 at 18:11
11

See "Introducing XML Serialization" in the MSDN documentation. Among other things, it says:

Items That Can Be Serialized

The following items can be serialized using the XmlSerializer class:

Public read/write properties and fields of public classes.

Classes that implement ICollection or IEnumerable.

Note:

Only collections are serialized, not public properties.
XmlElement objects.

XmlNode objects.

DataSet objects.

Also, see "Why XML-Serializable class need a parameterless constructor"


Also, IXmlSerializable

In addition to the above types which can be serialized by the XML Serializer, any type which implements the IXmlSerializable interface can be serialized and deserialized. In particular, this means that the XElement and XDocument types can be serialized.

See "IXmlSerializable Interface".

6

Limitation of XMLSerializer - Properties without setter can't be serialized.

But you can use DataContractSerializer to serialize private setter properties -

[DataMember]
public string Id
{
    get
    {
         return Guid.NewGuid().ToString();
    }
    private set {}
}
1
  • 1
    The property is supposed to be serialized as an xml-attribute (see original post). Your code (DataMember attribute) produces an xml-element. As far as I know there is no (clean) way to tell the DataContractSerializer to turn a property into an attribute and not an element. DataContractSerializer is nice but you always have to keep this limitation in mind.
    – sth_Weird
    Feb 10, 2016 at 8:38
2

if you want to have private setters, and have the object be serializable/deserializable, impliment ISerializable, and create a constructor like MyObject(SerializationInfo info, StreamingContext context). An example is found here.

0

Serialization attributes are used to serialize and deserialize objects. XmlSerializer will assume that you don't need to serialize any property that doesn't have a setter. Setter will be used when deserializing a string into an object, because an instance of the object needs to be created and then the setter will be used to populate the property value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.