locked
Export data in 2d array to XML file RRS feed

  • Question

  • I have a 2d Array.  I want to export the data in the array to an XML file.  Effictively each row in the array is record and the columns are fields.  I want to create an XML file and then export this data to this file.

    Can anyone tell me how to do this?

    Thanks

    EM
    Tuesday, September 9, 2008 8:59 PM

Answers

  • I'd suggest the use of serialization, but to do this you would have to move away from a 2d array as this isn't really supported using the standard XmlSerilaizer classes (I might be corrected here!). Here's what I would do but there are probably a whole host of other options:

    Firstly create a class for holding name value pairs....i.e. replacing the need for a 2d array:

    <Serializable()> _  
       Public Class NameValuePair  
     
        Private _Name As String  
        Private _Value As String  
     
        Public Sub New()  
     
            MyBase.New()  
     
        End Sub  
     
        Public Sub New(ByVal name As String, ByVal value As String)  
     
            MyBase.New()  
     
            With Me  
                ._Name = name 
                ._Value = value 
            End With  
     
        End Sub  
     
        Public Property Name() As String  
            Get  
                Return _Name  
            End Get  
            Set(ByVal value As String)  
                _Name = value 
            End Set  
        End Property  
     
        Public Property Value() As String  
            Get  
                Return _Value  
            End Get  
            Set(ByVal value As String)  
                _Value = value 
            End Set  
        End Property  
     
    End Class 

    Secondly creating a function that can serialize objects to an XMLDocument object i.e.

    Public Shared Function SerilaizeObjectToXML(ByVal objToSerialize As Object) As XmlDocument  
     
            Dim xmlDoc As New XmlDocument  
     
            If Not IsNothing(objToSerialize) Then  
     
                Using str As New IO.MemoryStream  
     
                    Dim xmlSerial As New System.Xml.Serialization.XmlSerializer(objToSerialize.GetType())  
     
                    xmlSerial.Serialize(str, objToSerialize)  
                    str.Position = 0 
                    xmlDoc.Load(str)  
     
                End Using  
     
            End If  
     
            Return xmlDoc  
     
        End Function 

    Thirdly and lastly creating an array (this could be a collection though i.e. list(of NameValuePair))  of the type NameValuePair and serilaizing it to XML.

            Dim nameValueArray(1) As NameValuePair  
     
            nameValueArray(0) = New NameValuePair("Column1", "Value1")  
            nameValueArray(1) = New NameValuePair("Column2", "Value2")  
     
            SerilaizeObjectToXML(nameValueArray).Save("c:\somefilename.xml") 'This line serializes array to XML and saves it to the file system

    Oh and you might be wanting to retrieve the XML back into an array at some point! This is where you take advantage of deserialization so you would have the following code to deserialize your XML back into an object:

    Public Shared Function DeserializeXML(Of ObjectType)(ByVal xml As XmlNode) As ObjectType  
     
            If Not IsNothing(xml) Then  
     
                Using sr As New IO.StringReader(xml.OuterXml())  
     
                    Using xtr As New XmlTextReader(sr)  
     
                        Dim serializer As New Serialization.XmlSerializer(GetType(ObjectType))  
     
                        Return CType(serializer.Deserialize(xtr), ObjectType)  
     
                    End Using  
     
                End Using  
     
            End If  
     
        End Function 

    And then rebuild your objects like this:

            Dim nameValueArrayFromXml() As NameValuePair  
            Dim nameValueXML As New XmlDocument  
     
            nameValueXML.Load("c:\somefilename.xml")  'load serialized xml into XML DOM
            nameValueArrayFromXml = DeserializeXML(Of NameValuePair())(nameValueXML) 'deserialize xml to array

    Hope this helps and isn't too confusing.......written it quickly as I'm off tut pub!

    Cheers

    Col
    • Proposed as answer by Colin Daly Monday, September 15, 2008 9:10 AM
    • Marked as answer by Martin Xie - MSFT Tuesday, September 16, 2008 2:51 AM
    Friday, September 12, 2008 3:40 PM

All replies

  • I'd suggest the use of serialization, but to do this you would have to move away from a 2d array as this isn't really supported using the standard XmlSerilaizer classes (I might be corrected here!). Here's what I would do but there are probably a whole host of other options:

    Firstly create a class for holding name value pairs....i.e. replacing the need for a 2d array:

    <Serializable()> _  
       Public Class NameValuePair  
     
        Private _Name As String  
        Private _Value As String  
     
        Public Sub New()  
     
            MyBase.New()  
     
        End Sub  
     
        Public Sub New(ByVal name As String, ByVal value As String)  
     
            MyBase.New()  
     
            With Me  
                ._Name = name 
                ._Value = value 
            End With  
     
        End Sub  
     
        Public Property Name() As String  
            Get  
                Return _Name  
            End Get  
            Set(ByVal value As String)  
                _Name = value 
            End Set  
        End Property  
     
        Public Property Value() As String  
            Get  
                Return _Value  
            End Get  
            Set(ByVal value As String)  
                _Value = value 
            End Set  
        End Property  
     
    End Class 

    Secondly creating a function that can serialize objects to an XMLDocument object i.e.

    Public Shared Function SerilaizeObjectToXML(ByVal objToSerialize As Object) As XmlDocument  
     
            Dim xmlDoc As New XmlDocument  
     
            If Not IsNothing(objToSerialize) Then  
     
                Using str As New IO.MemoryStream  
     
                    Dim xmlSerial As New System.Xml.Serialization.XmlSerializer(objToSerialize.GetType())  
     
                    xmlSerial.Serialize(str, objToSerialize)  
                    str.Position = 0 
                    xmlDoc.Load(str)  
     
                End Using  
     
            End If  
     
            Return xmlDoc  
     
        End Function 

    Thirdly and lastly creating an array (this could be a collection though i.e. list(of NameValuePair))  of the type NameValuePair and serilaizing it to XML.

            Dim nameValueArray(1) As NameValuePair  
     
            nameValueArray(0) = New NameValuePair("Column1", "Value1")  
            nameValueArray(1) = New NameValuePair("Column2", "Value2")  
     
            SerilaizeObjectToXML(nameValueArray).Save("c:\somefilename.xml") 'This line serializes array to XML and saves it to the file system

    Oh and you might be wanting to retrieve the XML back into an array at some point! This is where you take advantage of deserialization so you would have the following code to deserialize your XML back into an object:

    Public Shared Function DeserializeXML(Of ObjectType)(ByVal xml As XmlNode) As ObjectType  
     
            If Not IsNothing(xml) Then  
     
                Using sr As New IO.StringReader(xml.OuterXml())  
     
                    Using xtr As New XmlTextReader(sr)  
     
                        Dim serializer As New Serialization.XmlSerializer(GetType(ObjectType))  
     
                        Return CType(serializer.Deserialize(xtr), ObjectType)  
     
                    End Using  
     
                End Using  
     
            End If  
     
        End Function 

    And then rebuild your objects like this:

            Dim nameValueArrayFromXml() As NameValuePair  
            Dim nameValueXML As New XmlDocument  
     
            nameValueXML.Load("c:\somefilename.xml")  'load serialized xml into XML DOM
            nameValueArrayFromXml = DeserializeXML(Of NameValuePair())(nameValueXML) 'deserialize xml to array

    Hope this helps and isn't too confusing.......written it quickly as I'm off tut pub!

    Cheers

    Col
    • Proposed as answer by Colin Daly Monday, September 15, 2008 9:10 AM
    • Marked as answer by Martin Xie - MSFT Tuesday, September 16, 2008 2:51 AM
    Friday, September 12, 2008 3:40 PM
  • Thanks so much.  I find this topic rather cryptic at the best of times. 

    Thanks

    EM
    Friday, September 12, 2008 5:50 PM