We help IT Professionals succeed at work.

Don't serialize empty elements

onea
onea asked
on
I have some classes in VB.Net which i'm using to serialize and deserialize from and to XML.
In the serialized result, i want the emtpy (nothing) elements not to be rendered.

I have following class:

Imports System.Xml.Serialization

<XmlRootAttribute("GLTransactionLine")> _
Public Class EOL_GLTransactionLine
    <XmlAttribute("type")> _
    Public sType as String
    <XmlAttribute("linetype")> _
    Public sLineType as String
    <XmlAttribute("date")> _
    Public dDate as Date

    Public Sub New()
    End Sub

End Class

Open in new window


When i create a new object and serialize it without setting the properties, the date object is automatically rendered and filled with date 0001-01-01T00:00:00
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014

Commented:
A date needs to have a value. It can not be empty. One option is to use a NullAble(of Date)

Author

Commented:
I tried that one, but it still outputs a tag.

<date xsi:nil="true">

Imports System.Xml.Serialization

<XmlRootAttribute("GLTransactionLine")> _
Public Class EOL_GLTransactionLine
    <XmlElement("type")> _
    Public sType as String
    <XmlElement("linetype")> _
    Public sLineType as String
    <XmlElement("date")> _
    Public dDate as Nullable(Of Date)

    Public Sub New()
    End Sub

End Class

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:
And you don't want it? How would the deserialize reconstruct the object if a property is missing?

Author

Commented:
I send the xml to another service, i don't want to read it back in.
Most Valuable Expert 2012
Top Expert 2014
Commented: