XmlSerializer problem - help?

  • Thread starter Thread starter Julie
  • Start date Start date
J

Julie

Hi everyone,
I have a question about XmlSerializer. I have some code that serializes an
object to xml, but it has all the whitespace and new lines in the output.
How do i remove these?

Thanks in advance.

Public Shared Function Serialize(ByVal options As OptionsData) As String
Try
Dim loSerializer As Xml.Serialization.XmlSerializer
Dim loStringWriter As System.IO.StringWriter
Dim lsResult As String = String.Empty

If Not options Is Nothing Then
loSerializer = New
Xml.Serialization.XmlSerializer(options.GetType)
loSerializer.Serialize()
loStringWriter = New System.IO.StringWriter()
loSerializer.Serialize(loStringWriter, options)
loStringWriter.Flush()
lsResult = loStringWriter.ToString
loStringWriter.Close()
End If

Return lsResult
Catch ex As Exception
Throw ex
End Try
End Function
 
Well, i've worked it out :-)

but i have another question, i don't want to use any namespaces. I have set
the Namespaces = false on the xmlwriter, but it throws an exception that it
can't apply namespaces while it is false...

Any ideas?

Public Shared Function Serialize(ByVal options As
SchedulingDefinitionOptionsData) As String
Dim lsResult As String = String.Empty
Dim loSerializer As Xml.Serialization.XmlSerializer = Nothing
Dim loStringWriter As System.IO.StringWriter = Nothing
Dim loXmlWriter As Xml.XmlTextWriter = Nothing

If options Is Nothing Then Return lsResult

Try
loSerializer = New
Xml.Serialization.XmlSerializer(options.GetType)
loStringWriter = New System.IO.StringWriter()

loXmlWriter = New Xml.XmlTextWriter(loStringWriter)
loXmlWriter.Formatting = Xml.Formatting.None
loXmlWriter.Namespaces = False

loSerializer.Serialize(loXmlWriter, options)

lsResult = loStringWriter.ToString
Catch ex As Exception
Throw ex
Finally
If loStringWriter IsNot Nothing Then loStringWriter.Close()
End Try

Return lsResult
End Function
 
Back
Top