0

I'm writing a .NET Core 3.1 Client for a WCF Service. I generated a client with svcutil. All members in reference.cs have the following attribute:

[System.Xml.Serialization.XmlElementAttribute("property.name", IsNullable = true, Order = 0)]

Some elements should not be serialized in the messaged when they are null, i.e.: Currently all elements are included:

<criteria>
  <criteria1 xsi:nil="true"/>
  <criteria2>1</criteria2>
  <criteria3 xsi:nil="true"/>
</criteria>

The desired result:

<criteria>
  <criteria2>1</criteria2>
</criteria>

In .Net Framework I was able to add the attribute [DataMember(EmitDefaultValue=false)] to get the correct behavior, this is not supported in .NET Core.

2 Answers2

0

As far as I know, the EmitDefaultValue is supported in the DotNet Core.
https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datamemberattribute.emitdefaultvalue?view=netframework-4.8
we can decorate the data member with that attribute in the data contract located in the Reference.cs file.

   [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue =false)]
    public int Id
    {
        get
        {
            return this.IdField;
        }
        set
        {
            this.IdField = value;
        }
    }

Feel free to let me know if there is anything I can help with.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22
0

I have found the solution, the problem exists when using System.Xml.Serialization.XmlElementAttribute, it does not work in combination with DataMember. The solution for not serializing empty values with XmlElementAttribute is found here: Suppress Null Value Types from Being Emitted by XmlSerializer