ToArray

Imports System.Xml
Imports System.Runtime.CompilerServices


Module XmlDocumentExtensions

    <Extension()>
    Public Function ToArray(ByRef XmlDoc As XmlDocument) As Byte()
        Using ms As New MemoryStream
            XmlDoc.Save(ms)
            Return ms.ToArray()
        End Using
    End Function

    <Extension()>
    Public Sub Load(ByRef XmlDoc As XmlDocument, ByteArray As Byte())
        Using ms As New MemoryStream(ByteArray)
            XmlDoc.Load(ms)
        End Using
    End Sub

End Module
Example:
Dim xmldoc As New XmlDocument

xmldoc.Load("c:\temp\test.xml")

Dim Bytes As Byte() = xmldoc.ToArray()
' save, zip, send Bytes or something else
'...

Dim ReceivedBytes As Byte()
ReceivedBytes = ...
xmldoc.Load(ReceivedBytes)
xmldoc.Save("c:\temp\received.xml")



Description

Converting XmlDocument to byte array and vice versa

Details

Double click on the code to select all.

 

;