user852194
user852194

Reputation: 165

Ignore serialize some object fields at runtime (Dynamic Ignore)

I have a webmethod which return an object ASResponse :

    [WebMethod]
    public ASResponse test()
    {
         return new ASResponse ();
     }

Question :

Is it possible to customize the SOAP response by force the compiler do not serialize some ASResponse class fields ( if some condition are true) and allow them if some condition are false.

Samples :

<soap:Body>

<WrongCaseResponse xmlns="http://tempuri.org">

<WrongCaseResult>

<Length>5</Length>

<ID>125487</ID>

<Error>183</Error>

</WrongCaseResult>

</WrongCaseResponse>

</soap:Body>

==========================================================================

   <soap:Body>
    <SuccessCaseResponse xmlns="http://tempuri.org">
  <SuccessCaseResult>
        <Length>5</Length>
        <ID>125487</ID>
        <CallHome>5000</CallHome>
  </SuccessCaseResult>
</SuccessCaseResponse >
 </soap:Body>

Upvotes: 0

Views: 3086

Answers (4)

MichaelSo
MichaelSo

Reputation: 219

You can define some default values and it prevents the fields from being serialized. But it works only with XmlSerializer

[XmlElement, DefaultValue("")]
string data;

[XmlArray, DefaultValue(null)]
List<string> data;

Upvotes: 0

everag
everag

Reputation: 7672

Yup, mark the Properties you would like to ignore with [XmlIgnore]

Of course, if you have control over the ASResponse class (sorry i don't know very well the API)

-- EDIT

Sorry now I understood correctly the question. I don't know how to dynamicaly define it.

Upvotes: 0

Eddy
Eddy

Reputation: 5370

If I read the question correctly you want to hide properties if 'some condition' is true or false. The already mentioned XmlIgnoreAttribute will always exclude a property from serialization. If you are looking for a way to determine at runtime if you want to include or exclude a property have a look at the ShouldSerialize method here

Small example:

[XmlElement("visibility")]
public bool? Visibility { get; set; }
public bool ShouldSerializeVisibility() { return Visibility.HasValue; }

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503469

Are you looking for XmlIgnoreAttribute? Put this on your property and XML serialization will ignore it.

I'm not terribly hot on SOAP serialization, so it could be that you actually need SoapIgnoreAttribute instead... I can't honestly say I know the difference between them, but hopefully giving you both options, you'll be able to take it further :)

Upvotes: 1

Related Questions