序列化Keyedcollection,其中包含另一个keyedcollection

时间:2016-03-21 21:21:47

标签: vb.net nested xmlserializer keyedcollection

我最近开始使用来自vba的vb.net。

对于我计划的应用程序,我想创建一个存储另一个keyedcollection的KeyedCollection。原因是我有一种数据库,我希望能够存储不同数量的" Parametersets",一个现在未定义的" List_of_Parameters"其中存储了系数数组。 我的问题在于序列化。 当我运行XMLSerialization时,只有正确存储了最深层嵌套的元素。上面一级的元素被称为" Array_of_node"除了keyedcollection之外,所有变量都被忽略。

我曾预料到,而不是我会看到类名。此外,我本来期待看到这样的事情。

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

...

任何帮助都会非常感激, 最好的祝福, 约翰。

这是我得到的xml输出:

<?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>

这是我的数据库:

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

这是存储&#34; List_of_Coeffiencts&#34;的列表的类。对于不同的参数:

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

这是最终的&#34;系数列表&#34;

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

这是我的样本主程序:

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

1 个答案:

答案 0 :(得分:1)

试试这个....(新代码和类)

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

XML应该现在看起来像这样......

    <?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>