早上好,我这个项目的序列化问题。 如果idPropostaField为null,我希望xml标记如下:
<PropostaRFO/>
但此时XML标记具有以下结构:
<PropostaRFO IdProposta="0" />
跟随对象的类,由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
非常感谢