Escribir un XML - Urgente

26/01/2005 - 00:47 por Juan | Informe spam
Hola a tod@s

Estoy tratando de escribir el contenido de un arraylist lleno con objetos de
un tipo específico, pero no he podido.

Les agradecería cualquier tipo de ayuda... estaba usando


Dim objWriter As New Serialization.XmlSerializer(GetType(Contacto))
'Crear un objeto file de tipo StremWriter para almacenar el
documento xml
Dim objfile As New StreamWriter("Contacto.xml")
'Serializar y crear el documento XML
objWriter.Serialize(objfile, objContactos)
'Cerrar el archivo
objfile.Close()

Gracias por cualquier ayuda...

Preguntas similare

Leer las respuestas

#1 SqlRanger
29/01/2005 - 02:09 | Informe spam
Agrega el atributo <Serializable()> a las clases que quieras serializar:

<Serializable()> _
Public Class Contacto
Public Property Nombre As String
' etc
End Class

Primero, la clase contacto tiene que estar decorada con el atributo
<Serializable()>. Por ejemplo:

<Serializable()> _
Public Class Contacto
Private mNombre As String
Public Property Nombre() As String
Get
Return mNombre
End Get
Set(ByVal Value As String)
mNombre = Value
End Set
End Property
Private mDirección As String
Public Property Dirección() As String
Get
Return mDirección
End Get
Set(ByVal Value As String)
mDirección = Value
End Set
End Property
End Class

Luego, para serializar en XML un ArrayList lleno de contactos hay que
pasarle al constructor del serializador XML el tipo ArrayList como tipo
principal y contactos como tipo adicional. Por ejemplo:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Contactos As New ArrayList
Dim c1 As New Contacto
c1.Dirección = "Alcalá"
c1.Nombre = "Pepe"
Contactos.Add(c1)
Dim c2 As New Contacto
c2.Dirección = "C/ Gran Vía"
c2.Nombre = "María"
Contactos.Add(c2)
Dim serializer As New XmlSerializer(GetType(ArrayList), New Type()
{GetType(Contacto)})
Dim writer As New IO.StreamWriter("Contactos.xml")
serializer.Serialize(writer, Contactos)
writer.Close()
End Sub

Esto daría como resultado un archivo xml como este:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAnyType xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-in...">
<anyType xsi:type="Contacto">
<Nombre>Pepe</Nombre>
<Dirección>Alcalá</Dirección>
</anyType>
<anyType xsi:type="Contacto">
<Nombre>María</Nombre>
<Dirección>C/ Gran Vía</Dirección>
</anyType>
</ArrayOfAnyType>

Personalmente esto no me gusta demasiado, preferiría utilizar una colección
específica de contactos:

<Serializable()> _
Public Class ColeccionContactos
Inherits CollectionBase

Public Sub New()
MyBase.New()
End Sub

Default Public Property Item(ByVal index As Integer) As Contacto
Get
Return Me.List.Item(index)
End Get
Set(ByVal Value As Contacto)
Me.List.Item(index) = Value
End Set
End Property

Public Function Add(ByVal Contacto As Contacto) As Integer
Return Me.List.Add(Contacto)
End Function


End Class

Entonces ya sólo hace falta pasarle el tipo ColeccionContactos al
serializador Xml:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Contactos As New ColeccionContactos
Dim c1 As New Contacto
c1.Dirección = "Alcalá"
c1.Nombre = "Pepe"
Contactos.Add(c1)
Dim c2 As New Contacto
c2.Dirección = "C/ Gran Vía"
c2.Nombre = "María"
Contactos.Add(c2)
Dim serializer As New XmlSerializer(GetType(ColeccionContactos))
Dim writer As New IO.StreamWriter("Contactos.xml")
serializer.Serialize(writer, Contactos)
writer.Close()
End Sub

y el documento xml sería entonces:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfContacto xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-in...">
<Contacto>
<Nombre>Pepe</Nombre>
<Dirección>Alcalá</Dirección>
</Contacto>
<Contacto>
<Nombre>María</Nombre>
<Dirección>C/ Gran Vía</Dirección>
</Contacto>
</ArrayOfContacto>

Saludos:

Jesús López
MVP

"Juan" escribió en el mensaje
news:
Hola a

Estoy tratando de escribir el contenido de un arraylist lleno con objetos


de
un tipo específico, pero no he podido.

Les agradecería cualquier tipo de ayuda... estaba usando


Dim objWriter As New


Serialization.XmlSerializer(GetType(Contacto))
'Crear un objeto file de tipo StremWriter para almacenar el
documento xml
Dim objfile As New StreamWriter("Contacto.xml")
'Serializar y crear el documento XML
objWriter.Serialize(objfile, objContactos)
'Cerrar el archivo
objfile.Close()

Gracias por cualquier ayuda...

email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida