Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Serialization, No Whitespace

I have the following serialization method:

    Private Function SerializeData(ByVal data As cData) As String
        If data IsNot Nothing Then
            Dim xml_stream As New MemoryStream()
            Dim sr As StreamReader
            Dim xs As New XmlSerializer(GetType(cData))
            xml_stream = New MemoryStream()

            Try
                xs.Serialize(xml_stream, data)
                xml_stream.Position = 0

                sr = New StreamReader(xml_stream)
                Return sr.ReadToEnd()
            Finally
                If sr IsNot Nothing Then sr.Close()
                xml_stream.Close()
            End Try

        Else
            Return "No data"
        End If
    End Function

however it returns the xml the nested elements indented. Is there a way to shut that off on the serializer, or do I need to just run a find replace routine to get rid of it all?

Essentially, what I want to see is:

<root><child1>data</child1><child2>data</child2></root>
like image 224
Wes P Avatar asked Oct 07 '08 19:10

Wes P


People also ask

How to preserve whitespace in XML?

This behavior is based on the XSLT specification, which defines that the whitespace will be preserved if: An ancestor element of the text node has an xml:space attribute with a value of preserve , and no closer ancestor element has xml:space with a value of default .

What is serialization in XML?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

What is the difference between binary serialization and XML serialization?

Xml Serializer serializes only public member of object but Binary Serializer serializes all member whether public or private. In Xml Serialization, some of object state is only saved but in Binary Serialization, entire object state is saved.


1 Answers

Use the Serialize override that accepts an XmlWriter parameter. Create the XmlWriter using XmlWriter::Create, passing in an XmlWriterSettings object with the Indent property set to false.

like image 98
Kevin Dente Avatar answered Nov 03 '22 00:11

Kevin Dente