Volcar un objeto complejo de memoria a XML

Dim writer As New XmlTextWriter(“c:\binary\proyectos\bticket\volcadoSesiones.xml”, System.Text.Encoding.UTF8)
writer.WriteStartDocument(True)
writer.Formatting = Formatting.Indented
writer.Indentation = 2
writer.WriteStartElement(“Sesiones”)
‘Dim lst = CallByName(ListaSesiones, ListaSesiones.GetType.Name, CallType.Get)
For Each objSesion As CSesionVenta In ListaSesiones
CrearNodo(Of CSesionVenta)(writer, objSesion.GetType.Name, objSesion)
Next

writer.WriteEndElement()
writer.WriteEndDocument()
writer.Close()

Shared Sub CrearNodo(Of T)(ByRef writer As XmlTextWriter, ByVal nombreNodo As String, ByVal objeto As T)
writer.WriteStartElement(nombreNodo)
Dim propertiesU = objeto.GetType().GetProperties(Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public)
For Each Prop As Reflection.PropertyInfo In propertiesU
If Prop.CanRead Then

Dim bEsLista As Boolean = Prop.PropertyType.Name.Contains(“List`1”)

Dim nombre As String

Dim valor As String
nombre = Prop.Name
Try
If Not bEsLista Then
Dim mbEsClase As Boolean = False
Dim lst = CallByName(objeto, nombre, CallType.Get)
mbEsClase = lst.GetType.Namespace.Contains(“bTicket”)
If Not mbEsClase Then
writer.WriteStartElement(nombre)
valor = lst
writer.WriteString(valor)
writer.WriteEndElement()
Else

‘writer.WriteStartElement(nombreU)

‘For Each obj In lst
CrearNodo(writer, nombre, lst)
‘Next

‘writer.WriteEndElement()
End If

Else

Dim lst = CallByName(objeto, nombre, CallType.Get)
writer.WriteStartElement(nombre)

For Each obj In lst
CrearNodo(writer, obj.GetType.Name, obj)
Next

writer.WriteEndElement()

End If

Catch ex As Exception
writer.WriteStartElement(nombre)
valor = “Nothing”
writer.WriteString(valor)
writer.WriteEndElement()
End Try

End If
Next

writer.WriteEndElement()

End Sub