ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ToArray

Converting XmlDocument to byte array and vice versa

Source

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")

Author: Mike Lorenz

Submitted on: 8 nov. 2018

Language: VB

Type: System.Xml

Views: 4645