简体   繁体   English

C#XmlSerializer在反序列化时忽略元素

[英]C# XmlSerializer ignore element on deserialization

A default XmlSerializer (automatically provided by Visual Studio) is used with my web service. 我的Web服务使用了默认的XmlSerializer (由Visual Studio自动提供)。 I'm only using attributes to mark methods [WebMethod] and objects [Serialize()] , etc. 我仅使用属性来标记方法[WebMethod]和对象[Serialize()]等。

There is an object passed in both directions between the web service and a client. Web服务和客户端之间双向传递了一个对象。

One of the members (public MyClassType MyClass ;) of this object is meant to be single direction, one time only value sent to the client. 该对象的成员之一(public MyClassType MyClass ;)是单向的,只有一次将值发送给客户端。 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. 如果客户端返回此成员集,则我希望Web服务不要对其进行反序列化,以便反序列化的对象包含该成员的null值。

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. 做了一些研究,如果我使用的是非XmlSerializer ,那么我可以将“ OnDeserialization ”属性附加到对象中可以实现所需功能的方法上。

I'm trying to avoid this potential scenario. 我试图避免这种潜在的情况。 Web service sends the object to the client with the member set. Web服务将对象与成员集一起发送到客户端。 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. Web服务会做一些额外的工作,并将相同的对象发送回去(可能会稍作修改),但是成员的值未更改,因此不再是当前的,客户端应该看到一个NULL值,以防止客户端对此采取行动。再次保持不变。 The web service could set a new value for the member which the client should act on again. Web服务可以为成员设置一个新值,客户端应再次对该成员进行操作。

Is there a similarly easy way to do this for XmlSerializer ? XmlSerializer是否有类似的简便方法?

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: 这样做的快捷方法是在服务器端类上具有proxy属性,该属性返回MyClass的当前值,但在设置时丢弃当前值:

    [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] . 然后用[XmlIgnore]标记“ real”属性。

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 . 请注意,使用XmlAttributeOverrides的答案是不完整的,因为必须将序列化程序缓存在哈希表中,以防止严重的内存泄漏 For an example of such hashing, check XMLSerialize and EXCLUDE the base class . 有关此类哈希的示例,请检查XMLSerialize并排除基类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM