Omit Null fields during serialization

Topics: Web Api
Oct 15, 2011 at 10:45 PM

Hi Guys,

I m using Preview5 and want to use default xml and json serialization of webApi. Is there a way I can omit the null fields in output from the serialization. OR do you I have to write my own custom serializer?

-Henry

Oct 15, 2011 at 11:49 PM

You can use the attribute DataMember(EmitDefault = false).

On Oct 15, 2011 4:45 PM, "HenryBhavsar" <notifications@codeplex.com> wrote:

From: HenryBhavsar

Hi Guys,

I m using Preview5 and want to use default xml and json serialization of webApi. Is there a way I can omit the null fields in output from the serialization. OR do you I have to write my own custom serializer?

-Henry

Read the full discussion online.

To add a post to this discussion, reply to this email (wcf@discussions.codeplex.com)

To start a new discussion for this project, email wcf@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Oct 16, 2011 at 12:09 AM

It still output Empty element which I don't want to

<object [xsi:type=Item]>

....

<ItemDesc xsi:nil="true" />

...

I do not want <ItemDesc xsi:nil="true" /> to appear in output xml.

 

-Henry

Oct 16, 2011 at 12:55 AM

Are u using POCO classes with a DataContract attribute?

On Oct 15, 2011 6:09 PM, "henrybhavsar" <notifications@codeplex.com> wrote:

From: henrybhavsar

It still output Empty element which I don't want to

<object [xsi:type=Item]>

....

<ItemDesc xsi:nil="true" />

...

I do not want <ItemDesc xsi:nil="true" /> to appear in output xml.

-Henry

Read the full discussion online.

To add a post to this discussion, reply to this email (wcf@discussions.codeplex.com)

To start a new discussion for this project, email wcf@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Oct 16, 2011 at 3:34 AM

Yes

Oct 17, 2011 at 7:13 AM

Hi Henry,

Let me take a guess.  Does your class declared explicitly with [DataContract]?  I was able to repro your problem if I comment out the [DataContract] attribute:

    //[DataContract]
    public class Product
    {
        [DataMember(EmitDefaultValue=false)]
        public Product ItemDesc;
    }

If this is not the issue, do you mind sharing your DataContract class here so we can help you identify the problem?

Thanks,
Maggie Ying

Oct 17, 2011 at 7:22 AM

Hi Maggie,

My class is definately decorated with [DataContract]

the code is as below:


    [DataContract]
    public class Item: DCDataObject 
    {
      [DataMember(EmitDefaultValue = false)]       
public Int32? ItemID { get; set; } 

        ...more propteies
    }

all properties in DCDataObject has [XmlIngnore] attribute and the DCDataObject class has [XmlInclude(typeof(Item))] Attribute

 In the meantime, I might try some simple example to see it works.

 

Thanks

-Henry 

Oct 17, 2011 at 8:07 AM


Just want to make sure one more thing, are you setting UseDataContractSerializer to true in the XmlMediaTypeFormatter?

var config = new HttpConfiguration();
config.Formatters.XmlFormatter.UseDataContractSerializer = false;

Thanks,
Maggie Ying

Coordinator
Oct 17, 2011 at 5:11 PM

By default Web API uses the XmlSerializer for serializing XML. When using the XmlSerializer you need to annotate your properties with the DefaultValueAttribute if you don’t want default values to be serialized.

Alternatively, you can use the DataContractSerializer to serialize to XML, in which case you set EmitDefaultValue to false.

Daniel Roth

From: henrybhavsar [email removed]
Sent: Sunday, October 16, 2011 11:22 PM
To: Daniel Roth
Subject: Re: Omit Null fields during serialization [wcf:276021]

From: henrybhavsar

Hi Maggie,

My class is definately decorated with [DataContract]

the code is as below:


[DataContract]
public class Item: DCDataObject
{
[DataMember(EmitDefaultValue = false)]
public Int32? ItemID { get; set; }

...more propteies
}

all properties in DCDataObject has [XmlIngnore] attribute and the DCDataObject class has [XmlInclude(typeof(Item))] Attribute

In the meantime, I might try some simple example to see it works.

Thanks

-Henry