我最近开始使用来自 vba 的 vb.net。
对于我计划的应用程序,我想创建一个存储另一个 keyedcollection 的 KeyedCollection。原因是我有一种数据库,我希望能够在其中存储不同数量的“参数集”,即现在未定义数量的“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
这是存储不同参数集的“List_of_Coeffiencts”列表的类:
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
这是最终的“系数列表”
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