Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Serialization and null value - C#

Tags:

I have an object that I serialize to XML. If one of the properties in the object is null, then native serialization ignores to add this as the attribute. Is this normal behavior, if so, is there a way I can override it?

ex:

public class Test {   [XmlAttribute]   public string value {set; get; }    [XmlAttribute]   public string key {set; get; } } 

When value is null, i get

<Root>   <Test key="blah"> </Root> 
like image 756
DotnetDude Avatar asked Apr 02 '09 21:04

DotnetDude


People also ask

How do you represent null in XML?

In an XML document, the usual way to represent a null value is to leave the element or attribute empty. Some business messages use a special value to represent a null value: <price>-999</price> .

Can null be serialized?

We can force Gson to serialize null values via the GsonBuilder class. We need to call the serializeNulls() method on the GsonBuilder instance before creating the Gson object. Once serializeNulls() has been called the Gson instance created by the GsonBuilder can include null fields in the serialized JSON.

What is the correct way of using XML serialization?

XML Serialization Considerations Type identity and assembly information are not included. Only public properties and fields can be serialized. Properties must have public accessors (get and set methods). If you must serialize non-public data, use the DataContractSerializer class rather than XML serialization.

Is XML a serialization format?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.


1 Answers

XmlElement( IsNullable = true )

like image 86
Sunny Milenov Avatar answered Sep 19 '22 19:09

Sunny Milenov