In my WEB API i get the result in XML and Json. it works fine. But, When I collect data from DB some records is empty. While on conversion to xml or json, the result is different. Common Class for Output is.
public class items
{
[JsonProperty(PropertyName = "frequency")]
[XmlElement(ElementName = "frequency")]
public string Frequency { get; set; }
[JsonProperty(PropertyName = "modulation")]
[XmlElement(ElementName = "modulation")]
public string Modulation { get; set; }
}
Conversion takes place by
var Station = new items
{
Frequency = (mContent["frequency"] is DBNull) ? null : mContent["frequency"].ToString(),
Modulation = (mContent["modulation"] is DBNull) ? null : mContent["modulation"].ToString(),
}
For Json Result I get what I really want:
[{"items":[{"frequency":null,"modulation":null}]}]
The XML shows
<items>
<item/>
</items>
But I want
<items>
<item>
<frequency/>
<modulation/>
</item>
</items>
How could do this?
I think you probably want to use the XmlElementAttribute.IsNullable
property:
public class items
{
[JsonProperty(PropertyName = "frequency")]
[XmlElement(ElementName = "frequency", IsNullable = true)]
public string Frequency { get; set; }
[JsonProperty(PropertyName = "modulation")]
[XmlElement(ElementName = "modulation", IsNullable = true)]
public string Modulation { get; set; }
}
You should then get:
<items>
<item>
<frequency xsi:nil = "true" />
<modulation xsi:nil = "true" />
</item>
</items>
See more on this question at Stackoverflow