Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore a property during xml serialization but not during deserialization

In C#, how can I make XmlSerializer ignore a property during serialization but not during deserialization? (Or how do I do the same with Json.net?)

To prevent a property from being serialized, you can add the XmlIgnore attribute:

[XmlIgnore] public int FooBar {get;set;} 

This will cause the <FooBar> tag to be omitted during serialization.

However, this also means that the <FooBar> tag will be ignored during deserialization.

In my case, I accept an array of items from user in the request, and for each item user can specify an action property if they want to add, modify or delete the item. I want to use the same model object for GET list calls, and don't want to return this action property. I expect this would be a pretty common case.

Another use case: say you have a circle object

public class Circle {     public double Radius { get; set; } } 

and you modify it to add a diameter property

public class Circle2 {     public double Diameter { get; set; }     public double Radius { get { return Diameter / 2; } set { Diameter = value*2; } } } 

You may want to serialize only the diameter, but still be able to deserialize xml files in the old format that contain only the radius.

I did my research and didn't find anything, hence this question

Solution: I figured out the solution. I can add a ShouldSerialize property which always return false, details at this MSDN documentation

(this solution could be added as an actual answer if this question is reopened)

like image 827
Manoj Avatar asked Aug 14 '13 21:08

Manoj


People also ask

How do you ignore property serializable?

In XML Serializing, you can use the [XmlIgnore] attribute (System. Xml. Serialization. XmlIgnoreAttribute) to ignore a property when serializing a class.

What is XML ignore attribute?

Remarks. The XmlIgnoreAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object. If you apply the XmlIgnoreAttribute to any member of a class, the XmlSerializer ignores the member when serializing or deserializing an instance of the class.

Can I make XmlSerializer ignore the namespace on deserialization?

Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization.

What is XML serialization and deserialization?

Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an object and transport it over the Internet using HTTP between a client and a server. On the other end, deserialization reconstructs the object from the stream.


2 Answers

This is the solution outlined by Manoj:

If you want to suppress serialization of a specific property Foo, but still be able to deserialize it, you can add a method public bool ShouldSerializeFoo() that always returns false.

Example:

public class Circle2 {     public double Diameter { get; set; }     public double Radius { get { return Diameter / 2; } set { Diameter = value*2; } }      [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]     public bool ShouldSerializeRadius() {return false;} } 

This will cause the Radius to not be serialized, but still allow it to be deserialized.

This method has to be public for the XMLSerializer to find it, so in order to avoid polluting the namespace you can add the EditorBrowsable attribute to hide it from the IDE. Unfortunately this hiding only works if the assembly is referenced as a DLL in your current project, but not if you edit the actual project with this code.

like image 139
HugoRune Avatar answered Sep 20 '22 15:09

HugoRune


If you want to ignore the element at serialization with XmlSerializer, you can use XmlAttributeOverrides:

XmlAttributeOverrides overrides = new XmlAttributeOverrides(); XmlAttributes attribs = new XmlAttributes(); attribs.XmlIgnore = true; attribs.XmlElements.Add(new XmlElementAttribute("YourElementName")); overrides.Add(typeof(YourClass), "YourElementName", attribs);  XmlSerializer ser = new XmlSerializer(typeof(YourClass), overrides); ser.Serialize(... 
like image 26
fcuesta Avatar answered Sep 21 '22 15:09

fcuesta