0

Good morning, I'm having a problem with serialization this item. If idPropostaField is null I want the xml tag is as follows:

 <PropostaRFO/>

but in this moment the XML tag has the following structure:

 <PropostaRFO IdProposta="0" />

Following the object's class, created by xsd2code

<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1087.0"), _
 System.SerializableAttribute(), _
 System.ComponentModel.DesignerCategoryAttribute("code"), _
 System.Xml.Serialization.XmlRootAttribute("PropostaRFO", [Namespace]:="", IsNullable:=True)> _
Partial Public Class _PropostaRFO

    Private idPropostaField As Integer



    Private testoRapportoField As String

    Private Shared sSerializer As System.Xml.Serialization.XmlSerializer

    <System.Xml.Serialization.XmlAttributeAttribute()> _
    Public Property IdProposta() As Integer
        Get
            Return Me.idPropostaField
        End Get
        Set(value As Integer)
            Me.idPropostaField = value
        End Set
    End Property


    <System.Xml.Serialization.XmlAttributeAttribute()> _
    Public Property TestoRapporto() As String
        Get
            Return Me.testoRapportoField
        End Get
        Set(value As String)
            Me.testoRapportoField = value
        End Set
    End Property

    Private Shared ReadOnly Property Serializer() As System.Xml.Serialization.XmlSerializer
        Get
            If (sSerializer Is Nothing) Then
                sSerializer = New System.Xml.Serialization.XmlSerializer(GetType(_PropostaRFO))
            End If
            Return sSerializer
        End Get
    End Property

#Region "Serialize/Deserialize"

    '''<summary>
    '''Serializes current _PropostaRFO object into an XML document
    '''</summary>
    '''<returns>string XML value</returns>
    Public Overridable Function Serialize() As String
        Dim streamReader As System.IO.StreamReader = Nothing
        Dim memoryStream As System.IO.MemoryStream = Nothing
        Try
            memoryStream = New System.IO.MemoryStream()
            Serializer.Serialize(memoryStream, Me)
            memoryStream.Seek(0, System.IO.SeekOrigin.Begin)
            streamReader = New System.IO.StreamReader(memoryStream)
            Return streamReader.ReadToEnd
        Finally
            If (Not (streamReader) Is Nothing) Then
                streamReader.Dispose()
            End If
            If (Not (memoryStream) Is Nothing) Then
                memoryStream.Dispose()
            End If
        End Try
    End Function

    '''<summary>
    '''Deserializes workflow markup into an _PropostaRFO object
    '''</summary>
    '''<param name="xml">string workflow markup to deserialize</param>
    '''<param name="obj">Output _PropostaRFO object</param>
    '''<param name="exception">output Exception value if deserialize failed</param>
    '''<returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
    Public Overloads Shared Function Deserialize(ByVal xml As String, ByRef obj As _PropostaRFO, ByRef exception As System.Exception) As Boolean
        exception = Nothing
        obj = CType(Nothing, _PropostaRFO)
        Try
            obj = Deserialize(xml)
            Return True
        Catch ex As System.Exception
            exception = ex
            Return False
        End Try
    End Function

    Public Overloads Shared Function Deserialize(ByVal xml As String, ByRef obj As _PropostaRFO) As Boolean
        Dim exception As System.Exception = Nothing
        Return Deserialize(xml, obj, exception)
    End Function

    Public Overloads Shared Function Deserialize(ByVal xml As String) As _PropostaRFO
        Dim stringReader As System.IO.StringReader = Nothing
        Try
            stringReader = New System.IO.StringReader(xml)
            Return CType(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)), _PropostaRFO)
        Finally
            If (Not (stringReader) Is Nothing) Then
                stringReader.Dispose()
            End If
        End Try
    End Function

    '''<summary>
    '''Serializes current _PropostaRFO object into file
    '''</summary>
    '''<param name="fileName">full path of outupt xml file</param>
    '''<param name="exception">output Exception value if failed</param>
    '''<returns>true if can serialize and save into file; otherwise, false</returns>
    Public Overridable Overloads Function SaveToFile(ByVal fileName As String, ByRef exception As System.Exception) As Boolean
        exception = Nothing
        Try
            SaveToFile(fileName)
            Return True
        Catch e As System.Exception
            exception = e
            Return False
        End Try
    End Function

    Public Overridable Overloads Sub SaveToFile(ByVal fileName As String)
        Dim streamWriter As System.IO.StreamWriter = Nothing
        Try
            Dim xmlString As String = Serialize()
            Dim xmlFile As System.IO.FileInfo = New System.IO.FileInfo(fileName)
            streamWriter = xmlFile.CreateText
            streamWriter.WriteLine(xmlString)
            streamWriter.Close()
        Finally
            If (Not (streamWriter) Is Nothing) Then
                streamWriter.Dispose()
            End If
        End Try
    End Sub

    '''<summary>
    '''Deserializes xml markup from file into an _PropostaRFO object
    '''</summary>
    '''<param name="fileName">string xml file to load and deserialize</param>
    '''<param name="obj">Output _PropostaRFO object</param>
    '''<param name="exception">output Exception value if deserialize failed</param>
    '''<returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
    Public Overloads Shared Function LoadFromFile(ByVal fileName As String, ByRef obj As _PropostaRFO, ByRef exception As System.Exception) As Boolean
        exception = Nothing
        obj = CType(Nothing, _PropostaRFO)
        Try
            obj = LoadFromFile(fileName)
            Return True
        Catch ex As System.Exception
            exception = ex
            Return False
        End Try
    End Function

    Public Overloads Shared Function LoadFromFile(ByVal fileName As String, ByRef obj As _PropostaRFO) As Boolean
        Dim exception As System.Exception = Nothing
        Return LoadFromFile(fileName, obj, exception)
    End Function

    Public Overloads Shared Function LoadFromFile(ByVal fileName As String) As _PropostaRFO
        Dim file As System.IO.FileStream = Nothing
        Dim sr As System.IO.StreamReader = Nothing
        Try
            file = New System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read)
            sr = New System.IO.StreamReader(file)
            Dim xmlString As String = sr.ReadToEnd
            sr.Close()
            file.Close()
            Return Deserialize(xmlString)
        Finally
            If (Not (file) Is Nothing) Then
                file.Dispose()
            End If
            If (Not (sr) Is Nothing) Then
                sr.Dispose()
            End If
        End Try
    End Function
#End Region
End Class

Many thanks

Fabio
  • 31,528
  • 4
  • 33
  • 72
ruosco82
  • 31
  • 5
  • Possible duplicate of [Xml serialization - Hide null values](http://stackoverflow.com/questions/5818513/xml-serialization-hide-null-values) – Fabio Mar 27 '17 at 12:43
  • I try the possible solution,but don't work in .net 4.5 – ruosco82 Mar 27 '17 at 12:45
  • Solved! I add this code: _ Public ReadOnly Property IdPropostaSpecified() As Boolean Get Return idPropostaField > 0 End Get End Property – ruosco82 Mar 27 '17 at 14:23

0 Answers0