1

I have a method which has some members like createdby,createddate etc in InsuredGeneralInfo and InsuredAddrInfo which I don't want to serialize . I didn't add [datamember] property to those but still I see them in my response xml when I test it using wcf client. any reason?

Here is the declaration in Interface

public class InsuredInfo
{
    public InsuredGeneralInfo insuredGeneralInfo { get; set; }
    public InsuredAddressInfo insuredAddrInfo { get; set; }
}

[DataContract]
public class InsuredGeneralInfo
{
    [DataMember]
    public string FirstName { get; set; }
    [DataMember]
    public string MiddleInitial { get; set; }
    [DataMember]
    public string LastName { get; set; }
    [DataMember]
    public string Gender { get; set; }
    [DataMember]
    public string DOB { get; set; }
    [DataMember]
    public string SSN { get; set; }

    public string CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public string LastModifiedBy { get; set; }
    public DateTime LastModifiedDate { get; set; }
}

Output :

<insuredGeneralInfo>
      <FirstName>test</FirstName>
      <MiddleInitial>t</MiddleInitial>
      <LastName>test</LastName>
      <Gender>male</Gender>
      <DOB>12-25-1982</DOB>
      <SSN>123456789</SSN>
      <CreatedBy>ad</CreatedBy>
      <CreatedDate>2015-06-24T16:12:00</CreatedDate>
      <LastModifiedBy>ad</LastModifiedBy>
      <LastModifiedDate>2015-06-24T16:12:00</LastModifiedDate>
    </insuredGeneralInfo>
tom redfern
  • 30,562
  • 14
  • 91
  • 126
nimish jain
  • 141
  • 1
  • 13
  • What steps are you doing to get that XML you posted? – Scott Chamberlain Jun 24 '15 at 21:29
  • I am invoking a WCF service from WCF client and passing the input parameters . – nimish jain Jun 24 '15 at 21:33
  • No, I mean what tool are you using to get that output. The code you posted saves it to a database, not generate XML. – Scott Chamberlain Jun 24 '15 at 21:40
  • Try to remove the [DataContract] attribute and add [XmlIgnore] – Ricardo Pontual Jun 25 '15 at 03:07
  • @RicardoPontual and how will that help? The fact that the members are not marked with the DataMember attribute should be sufficient to prevent them from being serialized. – tom redfern Jun 25 '15 at 07:30
  • 1
    @nimish is the xml you posted taken from the service response message? Can you post the entire message? Is this a REST or SOAP service? – tom redfern Jun 25 '15 at 07:36
  • @TomRedfern I agree with you, just removing DataMember must be enough to prevent serialization, but as Nimish Jain said, that solution didn't resolve the problem, so include Xmlgnore will inform to serializer that you don't want to serialize that property. – Ricardo Pontual Jun 25 '15 at 12:53
  • XMLignore works with xml serialization. It doesn't work with datacontract serialization. This is a SOAP service. – nimish jain Jun 25 '15 at 14:01

1 Answers1

1

With WCF you can choose to use DataContract / Datamember or just use public properties.

If you use public properties without attributes then all public properties will be serialized. If you use DataContract / Datamember then only fields with DataContract / Datamember will be serialized.

In your case the root class InsuredInfo is not marked with DataContract / Datamember, so WCF serializes all public properties.

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • Even after I add the datacontract and datamemmber attributes to InsuredInfo , it is showing me createdby,createddate in response xml . – nimish jain Jun 25 '15 at 21:37