我有一个尝试过的列表、数组和所有不同的类型
我有一个可以序列化的项目列表
在c#中
public mySortableBindingList<Item> TransactionLines = new mySortableBindingList<Item>();
在 vb.net
Public WithEvents TransactionLines As New mySortableBindingList(Of Item)
xml输出是
<TransactionLines>
<Item>
<elements>
</item>
<Item>
<elements>
</item>
</TransactionLines>
我的序列化方法如下
在 C# 中
public string SerializeToXML<T>(T obj)
{
string XMLString = "";
try {
XmlSerializer mySerializer = new XmlSerializer(obj.GetType());
using (MemoryStream myMemStream = new MemoryStream()) {
mySerializer.Serialize(myMemStream, obj);
myMemStream.SeekToBegin();
XMLString = myMemStream.ReadToEnd(Encoding.UTF8);
}
} catch (Exception ex) {
}
return XMLString;
}
在 vb.net
Public Function SerializeToXML(Of T)(ByVal obj As T) As String
Dim XMLString As String = ""
Try
Dim mySerializer As XmlSerializer = New XmlSerializer(obj.GetType)
Using myMemStream As MemoryStream = New MemoryStream()
mySerializer.Serialize(myMemStream, obj)
myMemStream.SeekToBegin()
XMLString = myMemStream.ReadToEnd(Encoding.UTF8)
End Using
Catch ex As Exception
End Try
Return XMLString
End Function
待序列化列表
C#
[XmlElement(Order = 17)]
public mySortableBindingList<ItemSerial> Serials = new mySortableBindingList<ItemSerial>();
VB.net
<XmlElement(Order:=17)>
Public Serials As New mySortableBindingList(Of ItemSerial)
这是我的 xml 输出
<Serials>
<Serial>A</Serial>
<ExpiryDate>0001-01-01T00:00:00</ExpiryDate>
<Qty>1</Qty>
</Serials>
<Serials>
<Serial>B</Serial>
<ExpiryDate>0001-01-01T00:00:00</ExpiryDate>
<Qty>1</Qty>
</Serials>
我需要以下内容,请解释我可能做错了什么
<Serials>
<ItemSerial>
<Serial>A</Serial>
<ExpiryDate>0001-01-01T00:00:00</ExpiryDate>
<Qty>1</Qty>
</ItemSerial>
<ItemSerial>
<Serial>B</Serial>
<ExpiryDate>0001-01-01T00:00:00</ExpiryDate>
<Qty>1</Qty>
</ItemSerial>
</Serials>