Its reversing order of attributes (XmlElements) set in Serializer
class.
namespace Windstream.Business
{
[Serializable]
public class Order
{
public Windstream.Business.Broadband Broadband;
public Windstream.Business.POTS POTS;
public Order()
{
// Default Constructor.
}
// Order
private string orderrequestid;
public string OrderRequestId
{
get { return orderrequestid; }
set { orderrequestid = value; }
}
// CODE that instantiates Order, and calls XMLSerializer() .
Order _order = new Order();
_order.OrderRequestId = Session["OrderRequestId"].ToString();
_order.Broadband = new Broadband();
bbLineItem = new LineItem();
bbLineItem.ProductId =
int.Parse(hashBroadbandSpeed["ProductId"].ToString());
// ...
// ...
_order.Broadband.LineItems.LineItem = arrBBOrderItems;
bool bXml = XmlSerialize.ToXmlFile(_order);
public static class XmlSerialize
{
public static bool ToXmlFile(Object objToXml)
{
// XmlTextWriter xtw = new XtmTextWriter();
try
{
String _xmlFile;
_xmlFile = XmlFilePath();
StreamWriter strWriter = new StreamWriter(_xmlFile);
XmlSerializer xmlSerializer = new
XmlSerializer(typeof(Windstream.Business.Order));
xmlSerializer.Serialize(strWriter, objToXml);
}
}
OUTPUT:
<Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Broadband>
<LineItems>
<LineItem>
<Quantity />
<ItemName>Broadband 6 Mbps</ItemName>
<ProductId>112</ProductId>
<Price>29.99</Price>
<Description>Download Max. Speed of 6.0 Mbps, Upload Max Speed
of 384 Kbps. This pricing available for new customers only when
bundled with qualifying services.</Description>
<FixedRecurringPrice>R</FixedRecurringPrice>
</LineItem>
</LineItems>
</Broadband>
<OrderRequestId>1</OrderRequestId>
</Order>
Nicholas Paldino [.NET/C# MVP] - 24 Sep 2007 20:40 GMT
Fred,
The XmlSerializer does not have a defined order (AFAIK) if you do not
set it explicitly. You have to attach the XmlElement attribute to the
fields that are serialized and then set the Order property on those
attributes to the order you want them to appear in the output XML.
Also, you don't need the Serializable attribute for XML serialization.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Its reversing order of attributes (XmlElements) set in Serializer
> class.
[quoted text clipped - 72 lines]
> <OrderRequestId>1</OrderRequestId>
> </Order>