Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml Serialization - Render Empty Element

Tags:

I am using the XmlSerializer and have the following property in a class

public string Data { get; set; } 

which I need to be output exactly like so

<Data /> 

How would I go about achieving this?

like image 963
Jaimal Chohan Avatar asked Feb 24 '10 22:02

Jaimal Chohan


2 Answers

I was recently doing this and there is an alternative way to do it, that seems a bit simpler. You just need to initialise the value of the property to an empty string then it will create an empty tag as you required;

Data = string.Empty; 
like image 54
Firedragon Avatar answered Sep 29 '22 04:09

Firedragon


The solution to this was to create a PropertyNameSpecified property that the serializer uses to determine whether to serialize the property or not. For example:

public string Data { get; set; }  [XmlIgnore] public bool DataSpecified  {     get { return !String.IsNullOrEmpty(Data); }    set { return; } //The serializer requires a setter } 
like image 45
Jaimal Chohan Avatar answered Sep 29 '22 04:09

Jaimal Chohan