Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

XmlSerializer is reversing order of XmlElements in class serialized.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
FredZimmerman@gmail.com - 24 Sep 2007 20:26 GMT
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>

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.