0

A default XmlSerializer (automatically provided by Visual Studio) is used with my web service. I'm only using attributes to mark methods [WebMethod] and objects [Serialize()], etc.

There is an object passed in both directions between the web service and a client.

One of the members (public MyClassType MyClass;) of this object is meant to be single direction, one time only value sent to the client. If the client returns this member set, I want my web service to not deserialize it so that the deserialized object contains a null value for that member.

Did some research and if I were using non XmlSerializer, then I could attach a "OnDeserialization" attribute to a method within the object that would achieve what I need.

I'm trying to avoid this potential scenario. Web service sends the object to the client with the member set. The client acts on the member as it should. The client sends back the object with the member not cleared (NULLed) because it forgot. The web service does some additional work and sends the same object back (maybe slightly modified), but the member's value was not changed and therefore is no longer current and the client should see a NULL value for it to prevent the client from acting on this unchanged member again. The web service could set a new value for the member which the client should act on again.

Is there a similarly easy way to do this for XmlSerializer?

kaya3
  • 47,440
  • 4
  • 68
  • 97
skubany
  • 51
  • 1
  • 7
  • Do you want the member not to be *deserialized*, or not to be *set afterwards*? It's easy to prevent it from being set, it's hard to prevent it from being deserialized. – dbc Jan 27 '16 at 01:03
  • Not to be deserialized. Once the object comes back from the client, the member should be NULL at the time the object is usable in web service code again. – skubany Jan 27 '16 at 01:08
  • That means it's not set. My point is that it's easy to have your `MyClassType` object be deserialized *and then discarded and never set back*. – dbc Jan 27 '16 at 01:08
  • You mean "seNt back"? I'm going to update my question (if I can, since I'm new here) with a scenario that I'm trying to avoid. – skubany Jan 27 '16 at 01:13
  • No, I mean "set back". I.e. while deserializing on the server from the client, do not set the property value back. – dbc Jan 27 '16 at 01:19
  • And if you don't set it back then it'll be NULL. Then yes, that's what I need. But the server must still be able to set the value at some point outside of the serialization/deserialization process. So it's not the case that it can "never" be set again. – skubany Jan 27 '16 at 01:26

2 Answers2

1

The quick-and-dirty way to do this is to have a proxy property on the server side class that returns the current value of MyClass, but discards the current value when set:

    [XmlIgnore]
    public MyClassType MyClass { get; set; }

    [XmlElement("MyClass")]
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public MyClassType GetMyClassForXml
    {
        get
        {
            return MyClass;
        }
        set
        {
            // DO NOTHING
        }
    }

Then mark the "real" property with [XmlIgnore].

For a more complex solution, see Excluding some properties during serialization without changing the original class. Be aware that the answer that uses XmlAttributeOverrides is incomplete, as the serializer must be cached in a hash table to prevent a severe memory leak. For an example of such hashing, check XMLSerialize and EXCLUDE the base class.

dbc
  • 104,963
  • 20
  • 228
  • 340
0

I think you are looking for the XmlIgnoreAttribute.

Or perhaps the "Specified" suffix.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • What I need is an XmlIgnoreAttribute that applies when deserializing only. As it stands, it applies on serialization. I want the member to be serialized in the web service on the way out and I don't want it to be deserialized in the web service when coming back. – skubany Jan 27 '16 at 00:48