I need to serialize an Object to XML and back. The XML is fix and I can't change it. I fail to generate this structure after bookingList
.
How can I "group" these <booking>
elements to appear as a LIST and keep <error>
& <counter>
before this List of <booking>
elements.
See my example here:
Structure i need....
<nicexml> <key_id>1234567</key_id> <surname>Jil</surname> <name>Sander</name> <station_id>1</station_id> <ownBookings> <bookingList> <error></error> <counter>20</counter> <booking> <bookingID>1234567890</bookingID> </booking> <booking> <bookingID>2345678901</bookingID> </booking> </bookingList> </ownBookings> </nicexml>
Structure i get with C# code below....
<nicexml> <key_id>1234567</key_id> <surname>Jil</surname> <name>Sander</name> <station_id>1</station_id> <ownBookings> <bookingList> <booking> <booking> <bookingID>1234567890</bookingID> </booking> <booking> <bookingID>2345678901</bookingID> </booking> <booking> <error></error> <counter>20</counter> </bookingList> </ownBookings> </nicexml>
C# Code:
using System; using System.Xml.Serialization; using System.Collections.Generic; namespace xml_objects_serials { public class bookings { public class nicexml { public string key_id { get; set; } public string surname { get; set; } public string name { get; set; } public int station_id { get; set; } public ownBookings ownBookings { get; set; } } public class ownBookings { public bookingList bookingList { get; set; } } public class bookingList { public string error { get; set; } public int counter { get; set; } public List<booking> booking= new List<booking>(); } public class booking { public int bookingID { get; set; } } }
As with the CreatePo method, you must first construct an XmlSerializer, passing the type of the class to be deserialized to the constructor. Also, a FileStream is required to read the XML document. To deserialize the objects, call the Deserialize method with the FileStream as an argument.
XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.
Xml Serializer serializes only public member of object but Binary Serializer serializes all member whether public or private. In Xml Serialization, some of object state is only saved but in Binary Serialization, entire object state is saved.
XML serialization serializes only the public fields and property values of an object into an XML stream. XML serialization does not include type information. For example, if you have a Book object that exists in the Library namespace, there is no guarantee that it is deserialized into an object of the same type.
Try decorating the properties of the bookingList
class with the XmlElementAttribute, in order to control how the objects of that class are going to be serialized to XML.
Here's an example:
public class bookingList { [XmlElement(Order = 1)] public string error { get; set; } [XmlElement(Order = 2)] public int counter { get; set; } [XmlElement(ElementName = "booking", Order = 3)] public List<booking> bookings = new List<booking>(); } public class booking { public int id { get; set; } }
In my test I obtained this output:
<?xml version="1.0" ?> <bookingList> <error>sample</error> <counter>0</counter> <booking> <id>1</id> </booking> <booking> <id>2</id> </booking> <booking> <id>3</id> </booking> </bookingList>
Related resources:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With