本文介绍了List的序列化没有提供所需的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个尝试过的列表,数组和所有不同的类型 我有一个序列化的项目列表很好 in c# public List< Item> TransactionLines = new 列表< ItemSerial>(); in vb。 net 公共 WithEvents TransactionLines 作为 新列表( 项目) xml输出是 < ; TransactionLines > < 项目 > < 元素 > < / item > < 项目 > < 元素 > < / item > < / TransactionLines > 我的血清化方法如下 C# public string SerializeToXML< T>( T obj) { string XMLString = ; XmlSerializer mySerializer = new XmlSerializer(obj.GetType()); 使用(MemoryStream myMemStream = new MemoryStream()){ mySerializer.Serialize(myMemStream,obj); myMemStream.SeekToBegin(); XMLString = myMemStream.ReadToEnd(Encoding.UTF8); return XMLString; } 在vb.net 公共 功能 SerializeToXML( of T)( ByVal obj As T) As 字符串 Dim XMLString 作为 字符串 = Dim mySerializer As XmlSerializer = 新 XmlSerializer(obj。 GetType ) 使用 myMemStream As MemoryStream = N. ew MemoryStream() mySerializer.Serialize(myMemStream,obj) myMemStream.SeekToBegin() XMLString = myMemStream.ReadToEnd(Encoding.UTF8) 结束 使用 返回 XMLString 结束 功能 待序列表 c# [ XmlElement(Order = 17 )] public List< ItemSerial> Serials = new 列表< ItemSerial>(); vb.net < XmlElement(订单:= 17)> 公开序列号作为 新列表( ItemSerial) 这是我的xml输出 < 序列号 > < Serial > < / Serial > < ExpiryDate > 0001-01-01T00:00:00 < / ExpiryDate > < 数量 > 1 < /数量 > < / Serials > < Serials > < 串行 > B < / Serial > < ExpiryDate > 0001-01-01T00:00:00 < / ExpiryDate > < 数量 > 1 < / Qty > < / Serials > 需要以下输出。 < 序列号 > < ItemSerial > < Serial > < / Serial > < ExpiryDate > 0001-01-01T00:00:00 < / ExpiryDate > < 数量 > 1 < / Qty > < / ItemSerial > < ItemSerial > < Serial > B < / Serial > < ExpiryDate > 0001-01- 01T00:00:00 < / ExpiryDate > < 数量 > 1 < / Qty > < / ItemSerial > < / Serials > 请解释到目前为止我做错了什么以及要做什么。解决方案 通过使用XMLArray属性而不是XMLElement,它将根据需要进行序列化。 / BLOCKQUOTE> I have a tried list, array and all different typesI have a list of items which is serializing just finein c#public List<Item> TransactionLines = new List<ItemSerial>();in vb.netPublic WithEvents TransactionLines As New List(Of Item)xml output is <TransactionLines> <Item> <elements> </item> <Item> <elements> </item> </TransactionLines>my serilization method is the followingIn C#public string SerializeToXML<T>(T obj){ string XMLString = ""; XmlSerializer mySerializer = new XmlSerializer(obj.GetType()); using (MemoryStream myMemStream = new MemoryStream()) { mySerializer.Serialize(myMemStream, obj); myMemStream.SeekToBegin(); XMLString = myMemStream.ReadToEnd(Encoding.UTF8); return XMLString;}In vb.net Public Function SerializeToXML(Of T)(ByVal obj As T) As String Dim XMLString As String = "" 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 Return XMLStringEnd FunctionList to be serializedc#[XmlElement(Order = 17)]public List<ItemSerial> Serials = new List<ItemSerial>();vb.net <XmlElement(Order:=17)> Public Serials As New List(Of ItemSerial)This is my xml output <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>The following output is required. <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>Please explain what I did wrong so far and what is to be done. 解决方案 By using the XMLArray attribute rather than XMLElement it will be serialized as required. 这篇关于List的序列化没有提供所需的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 08:05