locked
Web API removes NULL values from response RRS feed

  • Question

  • User1490904075 posted

    Hi all,

    I am using xml formatter and returning xml by converting dataset to xml from WEB API. Web API removes NULL columns from the output.

    In json formatter we can have below code to include NULL values in output.

    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Include;

    But there is no equivalent properties in XmlFormatter. I've tried searching but not been able to find a solution so far.

    Please help. Thanks.

    Tuesday, May 3, 2016 7:07 AM

All replies

  • User-369506445 posted

    hi

    in your web api use below attribute:

    [XmlElement(IsNullable = true)]
    public string Title { get; set; }

    Tuesday, May 3, 2016 7:20 AM
  • User1490904075 posted

    Hi,

    Thanks for your reply. But I am not using classes.

    I am calling stored procedure and converting dataset to XML. I need a setting at application level. probably in webapi.config.

    Tuesday, May 3, 2016 7:26 AM
  • User-369506445 posted

    you can create a class and fill that with your store procedure and return the class or the class convert to xml

     

    Tuesday, May 3, 2016 7:36 AM
  • User1490904075 posted

    Actually that is not possible as there are many Stored procedures and there returns a lot of columns.

    Tuesday, May 3, 2016 7:45 AM
  • User-369506445 posted

    easy way is, that you n your stroe procdure check if field was empty replace with - or other symbol

    for example

    select ISNULL(yourColumn,'-') from myTable

    Tuesday, May 3, 2016 8:19 AM
  • User753101303 posted

    Hi,

    Not 100% sure what you are doing but according to https://social.msdn.microsoft.com/Forums/en-US/29f010ed-91dc-4a59-a31e-f427e648e65a/datasetwritexml-does-not-output-the-xsiniltrue-attribute?forum=xmlandnetfx a DataSet doesn't write NULL values to its XML representation (and it seems there is no way to change that).

    So I suspect that the problem is not really with the web api but rather with the dataset XML serialization. If you want to keep that (and the usual xsi:nil attribute) I'm afraid you'll have to roll your own (you could perhaps check http://referencesource.microsoft.com/ for the WriteXml Code).

    IMHO serializing a dataset is not the way to go if your intent is NOT to recreate a dataset on the other side.

    Tuesday, May 3, 2016 8:49 AM
  • User1490904075 posted

    Hi PatriceSc,

    Thanks for you reply. My aim is to get data from a stored procedure in a dataset and convert it to xml and return to the client application. Client application(web application) will convert it back to dataset, i.e to consume it.

    Tuesday, May 3, 2016 9:31 AM
  • User753101303 posted

    Ok so why are you trying to alter the DataSet serialization? I suspect you perhaps have some kind of issue that you are trying to fix by doing that? 

    I would suggest to explain first what is the issue you are facing when trying just to pass a DataSet around. To me it should just work without having to change this behavior. Knowing what happens would also allows to understand if we are facing the same issue if we want to give this a quick try to see which problem you may have encountered. Once again for now I would expect this to just work.

    Tuesday, May 3, 2016 11:40 AM
  • User1490904075 posted

    Hi 

    My issue was NULL columns were altogehter removed and when I converted those back to dataset code was throwing error because it did not find those columns. I fixed the issue by link provided by you in the last thread. I converted null values to empty string for each row before calling writexml. 

    Thanks for your time and help. Very much appreciated.

    Wednesday, May 4, 2016 5:05 AM
  • User753101303 posted

    I'm fairly sure the schema should be saved as well by default and that you should be able to recreate the DataSet with the same columns (even for empty tables for example).

    Check perhaps https://msdn.microsoft.com/en-us/library/system.data.dataset.schemaserializationmode(v=vs.110).aspx

    Wednesday, May 4, 2016 7:06 AM