简体   繁体   中英

Serialize Keyedcollection which contains another keyedcollection

I recently started working with vb.net coming from vba.

For my planned application I wanted to create a KeyedCollection which stores another keyedcollection. The reason is I have a kind of database, where I want to be able to store for a varying number of "Parametersets", an now undefined number of "List_of_Parameters" where arrays of coefficients are stored. My problem lies in serialization. When I run the XMLSerialization only the deepestly nested elements are stored correctly. The elements one level above are just called "Array_of_node" and all the variables ignored except the keyedcollection.

I had expected that instead of I would see the classname. Furthermore, I had expected to see something like this.

<Database>
   <Species>
     <Name>Parameterset 1</Name>
     <Node>...</Node>
     <Node>...</Node>
   </Species>

...

Any help would be really appreciated, Best regards, Johannes.

This is the xml-output I get:

<?xml version="1.0" encoding="utf-8"?>
<Database xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ArrayOfNode>
    <Node>
      <Name>ListOfParameters_1</Name>
      <value>
        <double>1</double>
        <double>2</double>
        <double>3</double>
      </value>
    </Node>
    <Node>
      <Name>ListOfParameters_2</Name>
      <value>
        <double>5</double>
        <double>6</double>
      </value>
    </Node>
  </ArrayOfNode>
  <ArrayOfNode>
    <Node>
      <Name>ListOfParameters_1</Name>
      <value>
        <double>7</double>
        <double>8</double>
        <double>9</double>
      </value>
    </Node>
    <Node>
      <Name>ListOfParameters_2</Name>
      <value>
        <double>10</double>
        <double>11</double>
      </value>
    </Node>
  </ArrayOfNode>
</Database>

This is my Database:

Imports System.Collections.ObjectModel
Imports TestListofList
Imports System.Xml.Serialization
Imports System.IO

<Serializable>
<XmlRootAttribute("Database")>
Public Class Database
    Inherits KeyedCollection(Of String, Species)

    Private myName As String
    Public Property Name() As String
        Get
            Return myName
        End Get
        Set(ByVal value As String)
            myName = value
        End Set
    End Property

    Protected Overrides Function GetKeyForItem(item As Species) As String
        Return item.Name
    End Function

    Sub New()
        MyBase.New()
    End Sub

    Sub New(ByVal name As String)
        Me.New()
        myName = name
    End Sub

    Public Sub SerializeToXML(ByVal filename As String)
        Dim locXMLWriter As New XmlSerializer(GetType(Database))
        Dim locXMLFile As New StreamWriter(filename)
        locXMLWriter.Serialize(locXMLFile, Me)
        locXMLFile.Flush()
        locXMLFile.Close()
    End Sub
End Class

This is the class which stores the List of "List_of_Coeffiencts" for the different parametersets:

Imports System.Collections.ObjectModel
Imports TestListofList
Imports System.Xml.Serialization

<Serializable>
Public Class Species
    Inherits KeyedCollection(Of String, Node)

    Public myName As String


    Public Property Name() As String
        Get
            Return myName
        End Get
        Set(ByVal value As String)
            myName = value
        End Set
    End Property

    Protected Overrides Function GetKeyForItem(item As Node) As String
        Return item.Name
    End Function

    Sub New()
        MyBase.New()
    End Sub
    Sub New(ByVal Name As String)
        Me.New()
        myName = Name
    End Sub
End Class

And this is the final "List of coefficients"

Public Class Node
    Private myName As String
    Private myvalue As Double()
    Public Property Name() As String
        Get
            Return myName
        End Get
        Set(ByVal value As String)
            myName = value
        End Set
    End Property

    Public Property value() As Double()
        Get
            Return myvalue
        End Get
        Set(ByVal value As Double())
            myvalue = value
        End Set
    End Property

    Sub New()

    End Sub

    Sub New(ByVal Name As String, value() As Double)
        myName = Name
        myvalue = value
    End Sub
End Class

And this my sample main program:

Module Module1

    Sub Main()
        Dim dot As Node
        Dim molecule As Species
        Dim data As New Database

        molecule = New Species("Parameterset1")

        data.Add(molecule)
        dot = New Node("ListOfParameters_1", New Double() {1, 2, 3})
        data.Item("Parameterset1").Add(dot)
        dot = New Node("ListOfParameters_2", New Double() {5, 6})
        data.Item("Parameterset1").Add(dot)

        molecule = New Species("Parameterset2")
        data.Add(molecule)
        dot = New Node("ListOfParameters_1", New Double() {7, 8, 9})
        data.Item("Parameterset2").Add(dot)
        dot = New Node("ListOfParameters_2", New Double() {10, 11})
        data.Item("Parameterset2").Add(dot)




        data.SerializeToXML("C:\test.xml")
    End Sub

End Module

Try this.... (New code and classes)

Imports System.IO
Imports System.Xml.Serialization

Module Module1

Sub Main()
    Dim Database1 As New List(Of Species)

    Dim Species1 As New Species
    Species1.Name = "SpeciesName1"
    Dim Parameterset1 As New Parameterset
    Parameterset1.Name = "Parameterset1"
    Parameterset1.Node.Add("1")
    Parameterset1.Node.Add("2")
    Parameterset1.Node.Add("3")
    Species1.Parameterset.Add(Parameterset1)

    Database1.Add(Species1)

    Dim Species2 As New Species
    Species2.Name = "SpeciesName2"
    Dim Parameterset2 As New Parameterset
    Parameterset2.Name = "Parameterset1"
    Parameterset2.Node.Add("1")
    Parameterset2.Node.Add("2")
    Species2.Parameterset.Add(Parameterset2)

    Database1.Add(Species2)

    ' to Serialize the object to test.xml
    Serialize(Database1)

    ' and to Deserialize from test.xml
    Dim Database2 As New List(Of Species)(Deserialize())
End Sub

Private Sub Serialize(SpeciesList As List(Of Species))

    ' Use a file stream here. 
    Using fs As New StreamWriter("test.xml")
        ' Construct a XmlSerializer and use it  
        ' to serialize the data to the stream. 
        Dim SerializerObj As New XmlSerializer(GetType(List(Of Species)))

        Try
            ' Serialize EmployeeList to the file stream
            SerializerObj.Serialize(fs, SpeciesList)
        Catch ex As Exception
            Console.WriteLine(String.Format("Failed to serialize. Reason: {0}", ex.Message))
        End Try
    End Using
End Sub

Private Function Deserialize() As List(Of Species)
    Dim EmployeeList2 = New List(Of Species)()

    ' Create a new file stream for reading the XML file
    Using fs = New StreamReader("test.xml")
        ' Construct a XmlSerializer and use it  
        ' to serialize the data from the stream. 
        Dim SerializerObj = New XmlSerializer(GetType(List(Of Species)))

        Try
            ' Deserialize the hashtable from the file
            EmployeeList2 = DirectCast(SerializerObj.Deserialize(fs), List(Of Species))
        Catch ex As Exception
            Console.WriteLine(String.Format("Failed to serialize. Reason: {0}", ex.Message))
        End Try
    End Using
    ' return the Deserialized data.
    Return EmployeeList2
End Function

End Module

<XmlRoot(ElementName:="Parameterset")>
Public Class Parameterset
    <XmlElement(ElementName:="Name")>
    Public Property Name As String

    <XmlElement(ElementName:="Node")>
    Public Property Node As List(Of String) = New List(Of String)
End Class

<XmlRoot(ElementName:="Species")>
Public Class Species

    <XmlElement(ElementName:="Name")>
    Public Property Name As String

    <XmlElement(ElementName:="Parameterset")>
    Public Property Parameterset As List(Of Parameterset) = New List(Of Parameterset)
End Class

<XmlRoot(ElementName:="Database")>
Public Class Database

    <XmlElement(ElementName:="Species")>
    Public Property Species As List(Of Species) = New List(Of Species)
End Class

The XML should look like this now.....

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfSpecies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Species>
        <Name>SpeciesName1</Name>
        <Parameterset>
          <Name>Parameterset1</Name>
          <Node>1</Node>
          <Node>2</Node>
          <Node>3</Node>
        </Parameterset>
      </Species>
      <Species>
        <Name>SpeciesName2</Name>
        <Parameterset>
          <Name>Parameterset1</Name>
          <Node>1</Node>
          <Node>2</Node>
        </Parameterset>
      </Species>
    </ArrayOfSpecies>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM