Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have a tried list, array and all different types
 
I have a list of items which is serializing just fine
 
in c#
public List<Item> TransactionLines = new List<ItemSerial>();
 
in vb.net
Public WithEvents TransactionLines As New List(Of Item)
 
xml output is
<TransactionLines>
       <Item>
        <elements>
       </item>
       <Item>
        <elements>
       </item>
    </TransactionLines>
 
my serilization method is the following
 
In 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 XMLString
End Function
 

List to be serialized
 
c#
[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.
Posted 30-Jan-13 23:35pm
EnzeroX669

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

By using the XMLArray attribute rather than XMLElement it will be serialized as required.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100