vb.net使用引用序列化/反序列化
我有两个不同对象的集合 假设我有以下两个对象vb.net使用引用序列化/反序列化,vb.net,serialization,xml-serialization,deserialization,Vb.net,Serialization,Xml Serialization,Deserialization,我有两个不同对象的集合 假设我有以下两个对象 Private col1作为(A的)集合和Private col2作为(B的)集合 但类型A的对象有一个类型B的集合作为属性 A看起来是这样的 Public Class A Public Property myStringProp() as string Public Property colB() as Collection(Of B) End Class 而B看起来像 Public Class B Public Prop
Private col1作为(A的)集合
和Private col2作为(B的)集合
但类型A的对象有一个类型B的集合作为属性
A看起来是这样的
Public Class A
Public Property myStringProp() as string
Public Property colB() as Collection(Of B)
End Class
而B看起来像
Public Class B
Public Property myStringProp() as string
End Class
因此,在col2中,我可以拥有例如20个B型项目。
在col1中,我有2个类型为A的项。每个项都有n个对col2集合的类型为B的项的引用
如何序列化和反序列化这些对象,以便在反序列化时恢复引用
首选使用XML进行序列化
我曾尝试使用DataContractSerializer,但不知道在何处以及如何使用它
编辑:
嗯。我可以手动解决这些问题。但我不喜欢这样:
For Each itema As A In col1
For Each itemb As B In itema.colB
For Each objB In col2
If itemb.myStringProp = objB.myStringProp Then
itemb = objB
End If
Next
Next
Next
这将在col1中遍历A的所有对象,然后遍历B的所有对象,并在col2中搜索具有相同myStringProp值的对象
因此,任何更清洁的解决方案都将受到欢迎:)
有没有更清洁的解决方案 序列化程序可以在单个序列化事件中保留对象引用。因此,如果将两个集合都作为单个对象的成员(然后对其进行序列化/反序列化),则可以在
DataContractSerializer
构造函数中使用preserveObjectReferences
参数,您将得到该参数。另一个选项是用
装饰类型,这也可以用来保留引用。下面的代码显示了第一种方法
Public Class StackOverflow_8387789
Public Class A
Public Property myStringProp() As String
Public Property colB() As Collection(Of B)
End Class
Public Class B
Public Property myStringProp() As String
End Class
Public Class Both
Public Property col1 As Collection(Of A)
Public Property col2 As Collection(Of B)
End Class
Public Shared Sub Test()
Dim both = New Both()
both.col2 = New Collection(Of B)
both.col2.Add(New B With {.myStringProp = "B1"})
both.col2.Add(New B With {.myStringProp = "B2"})
both.col2.Add(New B With {.myStringProp = "B3"})
both.col1 = New Collection(Of A)
Dim colBForA1 = New Collection(Of B)
colBForA1.Add(both.col2(0))
colBForA1.Add(both.col2(1))
Dim colBForA2 = New Collection(Of B)
colBForA2.Add(both.col2(1))
colBForA2.Add(both.col2(2))
both.col1.Add(New A With {.myStringProp = "A1", .colB = colBForA1})
both.col1.Add(New A With {.myStringProp = "A2", .colB = colBForA2})
Dim dcs = New DataContractSerializer(GetType(Both), Nothing, Integer.MaxValue, False, True, Nothing)
Dim ms = New MemoryStream()
Dim ws = New XmlWriterSettings With { _
.Encoding = Encoding.UTF8,
.Indent = True,
.IndentChars = " ",
.OmitXmlDeclaration = True
}
Dim xw = XmlWriter.Create(ms, ws)
dcs.WriteObject(xw, both)
xw.Flush()
Console.WriteLine("Serialized: {0}", Text.Encoding.UTF8.GetString(ms.ToArray()))
ms.Position = 0
Console.WriteLine("Now deserializing:")
Dim both2 = CType(dcs.ReadObject(ms), Both)
Console.WriteLine("Is both.col1(0).colB(0) = both.col2(0)? {0}", both2.col1(0).colB(0) Is both2.col2(0))
Console.WriteLine("Is both.col1(1).colB(1) = both.col2(2)? {0}", both2.col1(1).colB(1) Is both2.col2(2))
Console.WriteLine("Is both.col1(0).colB(0) = both.col2(2) (should be False)? {0}", both2.col1(0).colB(0) Is both2.col2(2))
End Sub
End Class
这就是我要找的。谢谢:)