Jaimal Chohan
Jaimal Chohan

Reputation: 8645

Xml Serialization - Render Empty Element

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?

Upvotes: 24

Views: 38988

Answers (6)

David Homer
David Homer

Reputation: 416

We've just had this issue with the Microsoft Group Policy XML format that does things in a strange way storing each day of the week a schedule runs as an empty XML Element.

I agree with Jaimal Chohan's method - you can snazzy it up a bit and make it more friendly with the PropertyGrid by making the backing String variable readonly and then setting it to null or an empty string based on the user accessible boolean variable.

    /// <summary>
    /// The schedule days.
    /// </summary>
    public class ScheduleDays:GlobalSerializableBase
    {


        /// <summary>
        /// Gets or sets the Monday backing variable.
        /// </summary>
        [ReadOnly(true)]
        [XmlElement("Monday")]
        public String MondayString
        {
            get { return _MondayString; }
            set { _MondayString = value; }
        }
        private String _MondayString = null;


        /// <summary>
        /// Gets or sets whether the schedule runs on Monday.
        /// </summary>
        [XmlIgnore]
        public bool Monday
        {
            get { return MondayString != null ; }
            set { MondayString = value ? String.Empty : null; }
        }


    }



}

Upvotes: 0

johnander11
johnander11

Reputation: 99

You could try adding the XMLElementAttribute like [XmlElement(IsNullable=true)] to that member and also set in the get/set property something like this:

[XmlElement(IsNullable = true)] 
public string Data 
{ 
    get { return string.IsNullOrEmpty(this.data) ? string.Empty : this.data; } 
    set 
    { 
        if (this.data != value) 
        { 
            this.data = value; 
        } 
    } 
} 
private string data;

And so you will not have:

<Data xsi:nil="true" />

You will have this on render:

<Data />

Upvotes: 2

Jaimal Chohan
Jaimal Chohan

Reputation: 8645

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
}

Upvotes: 11

Waleed A.K.
Waleed A.K.

Reputation: 1656

try to use public bool ShouldSerialize_PropertyName_(){} with setting the default value inside.

public bool ShouldSerializeData()
{
   Data = Data ?? "";
   return true;
}

Description of why this works can be found on MSDN.

Upvotes: 3

Firedragon
Firedragon

Reputation: 3733

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;

Upvotes: 36

HalloDu
HalloDu

Reputation: 907

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.

Upvotes: 1

Related Questions