XmlSerializer is reversing order of XmlElements in class serialized.

F

FredZimmerman

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>
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top