XmlSerialize - Serializing IEnumerable<T> with attributes at the top element

Post date: Jan 21, 2010 8:16:49 AM

using System;using System.Collections.Generic;using System.Text;using System.Xml.Serialization;using System.Collections;using System.Xml; namespace Henell.DataObjects

{

[XmlRoot("ROOT")] public class ROOT<T> : IXmlSerializable where T : IXmlSerializable, IRootNameable, new() { public ROOT() { _serializeObject = new T(); } public T SerializeObject { get { return _serializeObject; } set { _serializeObject = value; } } private T _serializeObject; #region IXmlSerializable Members public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "ROOT") { if (reader.ReadToDescendant(SerializeObject.XmlSerializationRootName)) { _serializeObject.ReadXml(reader); } else

throw new XmlException( string.Format("Error while parsing xml. Element '{0}' was not found as a descendant to element ROOT. The Xml is in a bad format" , SerializeObject.XmlSerializationRootName)); } } public void WriteXml(XmlWriter writer) { writer.WriteStartElement(SerializeObject.XmlSerializationRootName); _serializeObject.WriteXml(writer); writer.WriteEndElement(); } #endregion }}using System;using System.Collections.Generic;using System.Text;using System.Xml.Serialization;using System.Collections;using System.Xml;

namespace Henell.DataObjects

{ public interface IRootNameable { string XmlSerializationRootName { get; } }}using System;using System.Collections.Generic;using System.Text;using System.Xml.Serialization;

namespace Henell.DataObjects

{ public static class DeserializationManager

{ public static T Deserialize<T>(string xmlstring) where T : IRootNameable, IXmlSerializable, new() { T obj = new T(); System.IO.Stream mstream = new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(xmlstring)); System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ROOT<T>)); obj = ((ROOT<T>)serializer.Deserialize(mstream)).SerializeObject; return obj; } }}using System;using System.Collections.Generic;using System.Text;using System.Xml.Serialization;using System.Collections;using System.Xml;

namespace Henell.DataObjects

{ public class NewOrder : IEnumerable<OrderLine>, IXmlSerializable, IRootNameable

{ private string _orderWay; [XmlAttribute] public string OrderWay { get { return _orderWay; } set { _orderWay = value; } } private string _paymentTerm; [XmlAttribute] public string PaymentTerm { get { return _paymentTerm; } set { _paymentTerm = value; } } private int _productId; [XmlAttribute] public int ProductID

{ get { return _productId; } set { _productId = value; } } private int _customerID; [XmlAttribute] public int CustomerID

{ get { return _customerID; } set { _customerID = value; } } private int _dadCode; [XmlAttribute] public int DadCode

{ get { return _dadCode; } set { _dadCode = value; } } string _refPrint; [XmlAttribute] public string RefPrint { get { return _refPrint; } set { _refPrint = value; } } string _triggerCode; [XmlAttribute] public string TriggerCode { get { return _triggerCode; } set { _triggerCode = value; } } public OrderLine this[int i] { get { return (OrderLine)_orders[i]; } } public void Add(OrderLine ol) { _orders.Add(ol); } private List<OrderLine> _orders = new List<OrderLine>(); public List<OrderLine> Orders

{ get { return _orders; } set { _orders = value; } } #region IEnumerable<OrderLine> Members public IEnumerator<OrderLine> GetEnumerator() { foreach (OrderLine data in _orders) { yield return data; } } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() { foreach (OrderLine data in _orders) { yield return data; } } #endregion #region IXmlSerializable Members public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void ReadXml(System.Xml.XmlReader reader) { if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "Order") { this._customerID = int.Parse(reader["CustomerID"]); this._dadCode = int.Parse(reader["DadCode"]); this._orderWay = reader["OrderWay"]; this._paymentTerm = reader["PaymentTerm"]; this._productId = int.Parse(reader["ProductID"]); this._refPrint = reader["RefPrint"]; this._triggerCode = reader["TriggerCode"]; if (reader.ReadToDescendant("OrderLine")) { while (reader.MoveToContent() == XmlNodeType.Element)// && reader.LocalName == "OrderLine") { OrderLine ol = new OrderLine(); ol.ReadXml(reader); _orders.Add(ol); } } reader.Read(); } } public void WriteXml(System.Xml.XmlWriter writer) { //writer.WriteStartElement("Order"); writer.WriteAttributeString("CustomerID", this._customerID.ToString()); writer.WriteAttributeString("DadCode", this._dadCode.ToString()); writer.WriteAttributeString("OrderWay", this._orderWay.ToString()); writer.WriteAttributeString("PaymentTerm", this._paymentTerm.ToString()); writer.WriteAttributeString("ProductID", this._productId.ToString()); writer.WriteAttributeString("RefPrint", this._refPrint.ToString()); writer.WriteAttributeString("TriggerCode", this._triggerCode.ToString()); foreach (OrderLine o in _orders) { writer.WriteStartElement("OrderLine"); o.WriteXml(writer); writer.WriteEndElement(); } //'writer.WriteEndElement(); } #endregion #region IRootNameAble Members public string XmlSerializationRootName { get { return "Order"; } } #endregion }}using System;using System.Collections.Generic;using System.Text;using System.Xml.Serialization;using System.Collections;using System.Xml;

namespace Henell.DataObjects

{ public class OrderLine : IXmlSerializable

{ private int _quantity; [XmlAttribute] public int Quantity

{ get { return _quantity; } set { _quantity = value; } } private string _booklet; [XmlAttribute] public string Booklet { get { return _booklet; } set { _booklet = value; } } #region IXmlSerializable Members public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void ReadXml(System.Xml.XmlReader reader) { if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "OrderLine") { //System.Diagnostics.Trace.WriteLine(reader.LocalName); _booklet = reader["Booklet"]; _quantity = int.Parse(reader["Quantity"]); reader.Read(); } } public void WriteXml(System.Xml.XmlWriter writer) { writer.WriteAttributeString("Booklet", this._booklet); writer.WriteAttributeString("Quantity", this._quantity.ToString()); } #endregion }}using System;using System.Collections.Generic;using System.Text;using System.Xml.Serialization;using System.Xml;

namespace Henell.DataObjects

{ public class DeliveryAddress : IXmlSerializable, IRootNameable

{ string _customerID; bool _differentAddr; string _name; string _addr1; string _addr2; string _addr3; string _zip; string _city; string _country; string _phone; string _fax; string _discount; string _extraFee; string _fee; string _fee_amount; string _carrier; string _language_code; #region IXmlSerializable Members public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void ReadXml(System.Xml.XmlReader reader) { if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == XmlSerializationRootName) { this.CustomerID = reader["CustomerID"]; this.DifferentAddr = reader["DifferentAddr"] == "1" ? true : false ; this.Name = reader["Name"]; this.Addr1 = reader["Addr1"]; this.Addr2 = reader["Addr2"]; this.Addr3 = reader["Addr3"]; this.Zip = reader["zip"]; this.City = reader["city"]; this.Country = reader["country"]; this.Phone = reader["phone"]; this.ExtraFee = reader["ExtraFee"]; this.Fee = reader["Fee"]; this.Carrier = reader["Carrier"]; this.Language = reader["Language"]; //if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "Order") //{ // this._customerID = int.Parse(reader["CustomerID"]); // this._dadCode = int.Parse(reader["DadCode"]); // this._orderWay = reader["OrderWay"]; // this._paymentTerm = reader["PaymentTerm"]; // this._productId = int.Parse(reader["ProductID"]); // this._refPrint = reader["RefPrint"]; // this._triggerCode = reader["TriggerCode"]; // if (reader.ReadToDescendant("OrderLine")) // { // while (reader.MoveToContent() == XmlNodeType.Element)// && reader.LocalName == "OrderLine") // { // OrderLine ol = new OrderLine(); // ol.ReadXml(reader); // _orders.Add(ol); // } // } // reader.Read(); //} } } public void WriteXml(System.Xml.XmlWriter writer) { //writer.WriteStartElement("DeliveryAddress"); writer.WriteAttributeString("CustomerID", this.CustomerID); writer.WriteAttributeString("DifferentAddr", this.DifferentAddr ? "1" : "0"); writer.WriteAttributeString("Name", this.Name); writer.WriteAttributeString("Addr1", this.Addr1); writer.WriteAttributeString("Addr2", this.Addr2); writer.WriteAttributeString("Addr3", this.Addr3); writer.WriteAttributeString("zip", this.Zip); writer.WriteAttributeString("city", this.City); writer.WriteAttributeString("country", this.Country); writer.WriteAttributeString("phone", this.Phone); writer.WriteAttributeString("ExtraFee", this.ExtraFee); writer.WriteAttributeString("Fee", this.Fee); writer.WriteAttributeString("Carrier", this.Carrier); writer.WriteAttributeString("Language", this.Language); //writer.WriteEndElement(); } #endregion public string Addr1 { get { return _addr1; } set { _addr1 = value; } } public string Addr2 { get { return _addr2; } set { _addr2 = value; } } public string Addr3 { get { return _addr3; } set { _addr3 = value; } } public string Carrier { get { return _carrier; } set { _carrier = value; } } public string City { get { return _city; } set { _city = value; } } public string Country { get { return _country; } set { _country = value; } } public string CustomerID { get { return _customerID; } set { _customerID = value; } } public bool DifferentAddr { get { return _differentAddr; } set { _differentAddr = value; } } public string Discount { get { return _discount; } set { _discount = value; } } public string ExtraFee { get { return _extraFee; } set { _extraFee = value; } } public string Fax { get { return _fax; } set { _fax = value; } } public string Fee_amount { get { return _fee_amount; } set { _fee_amount = value; } } public string Fee { get { return _fee; } set { _fee = value; } } public string Language { get { return _language_code; } set { _language_code = value; } } public string Name { get { return _name; } set { _name = value; } } public string Phone { get { return _phone; } set { _phone = value; } } public string Zip { get { return _zip; } set { _zip = value; } } #region IRootNameAble Members public string XmlSerializationRootName { get { return "DeliveryAddress"; } } #endregion }}using System;using System.Collections.Generic;using System.Text;using System.Xml;using System.Xml.Serialization;using Henell.DataObjects;

namespace Henell.DataObjects

{ public class NewCustomer : IXmlSerializable, IRootNameable

{ #region Private Fields string _CompanyName; string _OrgNo; string _Addr1; string _Addr2; string _Addr3; string _zip; string _city; string _country; string _phone; string _fax; string _DeliveryDelay; string _PaymentMethod; string _Ctt_FirstName; string _Ctt_Name; string _Ctt_InternalPosition; string _Ctt_Phone; string _Ctt_Fax; string _Ctt_email; string _Bill_to_Name; string _Bill_to_addr1; string _Bill_to_addr2; string _Bill_to_addr3; string _Bill_to_zip; string _Bill_to_city; string _Bill_to_country;

#endregion #region IRootNameable Members public string XmlSerializationRootName { // For some reason it says "Company" in the specification "TRI2 - DA - SW200912-03.doc" get { return "Company"; } } #endregion #region IXmlSerializable Members public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == XmlSerializationRootName) { this._CompanyName = reader["CompanyName"]; this._OrgNo = reader["OrgNo"]; this._Addr1 = reader["Addr1"]; this._Addr2 = reader["Addr2"]; this._Addr3 = reader["Addr3"]; this._zip = reader["zip"]; this._city = reader["city"]; this._country = reader["country"]; this._phone = reader["phone"]; this._fax = reader["fax"]; this._DeliveryDelay = reader["DeliveryDelay"]; this._PaymentMethod = reader["PaymentMethod"]; this.Ctt_FirstName = reader["Ctt_FirstName"]; this._Ctt_Name = reader["Ctt_Name"]; this._Ctt_InternalPosition = reader["Ctt_InternalPosition"]; this._Ctt_Phone = reader["Ctt_Phone"]; this._Ctt_Fax = reader["Ctt_Fax"]; this._Ctt_email = reader["Ctt_email"]; this._Bill_to_Name = reader["Bill_to_Name"]; this._Bill_to_addr1 = reader["Bill_to_addr1"]; this._Bill_to_addr2 = reader["Bill_to_addr2"]; this._Bill_to_addr3 = reader["Bill_to_addr3"]; this._Bill_to_zip = reader["Bill_to_zip"]; this._Bill_to_city = reader["Bill_to_city"]; this._Bill_to_country = reader["Bill_to_country"]; } } public void WriteXml(XmlWriter writer) { writer.WriteAttributeString("CompanyName", this._CompanyName); writer.WriteAttributeString("OrgNo", this._OrgNo); writer.WriteAttributeString("Addr1", this._Addr1); writer.WriteAttributeString("Addr2", this._Addr2); writer.WriteAttributeString("Addr3", this._Addr3); writer.WriteAttributeString("zip", this._zip); writer.WriteAttributeString("city", this._city); writer.WriteAttributeString("country", this._country); writer.WriteAttributeString("phone", this._phone); writer.WriteAttributeString("fax", this._fax); writer.WriteAttributeString("DeliveryDelay", this._DeliveryDelay); writer.WriteAttributeString("PaymentMethod", this._PaymentMethod); writer.WriteAttributeString("Ctt_FirstName", this._Ctt_FirstName); writer.WriteAttributeString("Ctt_Name", this._Ctt_Name); writer.WriteAttributeString("Ctt_InternalPosition", this._Ctt_InternalPosition); writer.WriteAttributeString("Ctt_Phone", this._Ctt_Phone); writer.WriteAttributeString("Ctt_Fax", this._Ctt_Fax); writer.WriteAttributeString("Ctt_email", this._Ctt_email); writer.WriteAttributeString("Bill_to_Name", this._Bill_to_Name); writer.WriteAttributeString("Bill_to_addr1", this._Bill_to_addr1); writer.WriteAttributeString("Bill_to_addr2", this._Bill_to_addr2); writer.WriteAttributeString("Bill_to_addr3", this._Bill_to_addr3); writer.WriteAttributeString("Bill_to_zip", this._Bill_to_zip); writer.WriteAttributeString("Bill_to_city", this._Bill_to_city); writer.WriteAttributeString("Bill_to_country", this._Bill_to_country); } #endregion #region Public Properties public string Addr1 { get { return _Addr1; } set { _Addr1 = value; } } public string Addr2 { get { return _Addr2; } set { _Addr2 = value; } } public string Addr3 { get { return _Addr3; } set { _Addr3 = value; } } public string Bill_to_addr1 { get { return _Bill_to_addr1; } set { _Bill_to_addr1 = value; } } public string Bill_to_addr2 { get { return _Bill_to_addr2; } set { _Bill_to_addr2 = value; } } public string Bill_to_addr3 { get { return _Bill_to_addr3; } set { _Bill_to_addr3 = value; } } public string Bill_to_city { get { return _Bill_to_city; } set { _Bill_to_city = value; } } public string Bill_to_country { get { return _Bill_to_country; } set { _Bill_to_country = value; } } public string Bill_to_Name { get { return _Bill_to_Name; } set { _Bill_to_Name = value; } } public string Bill_to_zip { get { return _Bill_to_zip; } set { _Bill_to_zip = value; } } public string City { get { return _city; } set { _city = value; } } public string Country { get { return _country; } set { _country = value; } } public string Ctt_email { get { return _Ctt_email; } set { _Ctt_email = value; } } public string Ctt_Fax { get { return _Ctt_Fax; } set { _Ctt_Fax = value; } } public string Ctt_FirstName { get { return _Ctt_FirstName; } set { _Ctt_FirstName = value; } } public string Ctt_InternalPosition { get { return _Ctt_InternalPosition; } set { _Ctt_InternalPosition = value; } } public string Ctt_Name { get { return _Ctt_Name; } set { _Ctt_Name = value; } } public string Ctt_Phone { get { return _Ctt_Phone; } set { _Ctt_Phone = value; } } public string DeliveryDelay { get { return _DeliveryDelay; } set { _DeliveryDelay = value; } } public string Fax { get { return _fax; } set { _fax = value; } } public string OrgNo { get { return _OrgNo; } set { _OrgNo = value; } } public string PaymentMethod { get { return _PaymentMethod; } set { _PaymentMethod = value; } } public string Phone { get { return _phone; } set { _phone = value; } } public string CompanyName { get { return _CompanyName; } set { _CompanyName = value; } } public string Zip { get { return _zip; } set { _zip = value; } }

#endregion }}

Can De/serialize an xml document like the following:

<ROOT> <Order OrderWay="way_value" PaymentTerm="term_value" ProductID="product_value" CustomerID="customer_value" DadCode="dad_value" RefPrint="reference_value" TriggerCode="TriggerCode_value" > <OrderLine Booklet = "booklet_valu1e" Quantity ="quantity_value1"></OrderLine> <OrderLine Booklet = "booklet_valu1e" Quantity ="quantity_value1"></OrderLine> <OrderLine Booklet = "booklet_valu1e" Quantity ="quantity_value1"></OrderLine> <OrderLine Booklet = "booklet_valu1e" Quantity ="quantity_value1"></OrderLine> </Order></ROOT>