views:

30

answers:

2

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?

A: 

You could try adding the XMLElementAttribute like [XmlElement(IsNullable=true)] to that member. That will force the XML Serializer to add the element even if it is null.

HalloDu
But won't this render <Data xsi:nil="true" />
Jaimal Chohan
A: 

The soltuion to this was to create a Specified Property that the serializer uses to determine weather to serialze the property or not

public string Data { get; set; }

[XmlIgnore]
public string DataSpecified 
{ 
   get { return !String.IsNullOrEmpty(Data); }
   set { return; } //The serliazer requires a setter
}
Jaimal Chohan