?
Solved

xmlc# cml Serializing Order

Posted on 2009-04-24
11
Medium Priority
?
234 Views
Last Modified: 2012-05-06
Hi i am generating xml from c# classes.After generating i loose the order of tags in them.

InvoiceHeader>
    <SuppliersAddress>
      <AddressLine1>zzl</AddressLine1>
      <AddressLine2>zz</AddressLine2>
      <AddressLine3>zz</AddressLine3>
      <AddressLine4>zz</AddressLine4>
      <AddressLine5 />
      <PostCode>zzL</PostCode>
    </SuppliersAddress>
    <CustomerAddress>
      <AddressLine1>zz</AddressLine1>
      <AddressLine2>zz</AddressLine2>
      <AddressLine3>zz</AddressLine3>
      <AddressLine4>zz</AddressLine4>
      <AddressLine5>zz</AddressLine5>
      <PostCode>zz</PostCode>
    </CustomerAddress>
    <DeliveryDetail>
      <DeliveryLocationReference>N/A</DeliveryLocationReference>
      <DeliveryName>Same as invoice address</DeliveryName>
    </DeliveryDetail>
    <SuppliersName>zz</SuppliersName>
    <SupplierVATRegNo>zz4</SupplierVATRegNo>
    <CustomerName>zz</CustomerName>
    <InvoiceReference>zz</InvoiceReference>
    <InvoiceDate>zz</InvoiceDate>
    <TaxPointDate>zz4</TaxPointDate>
    <Currency>zz</Currency>
  </InvoiceHeader>
 
************************
 public class InvoiceHeader
    {
        private string _SuppliersName;
        public Address  SuppliersAddress;
        private string _SupplierVATRegNo;
        private string _CustomerName;
        public Address  CustomerAddress;
        private string _InvoiceReference;
        private string _InvoiceDate;
        private string _TaxPointDate;
        public DeliveryDetail DeliveryDetail;
        private string _Currency;
        private string _MatchingStatus;
        private string _HeaderExtraData;
 
        public string SuppliersName
        {
            set { _SuppliersName = value; }
            get { return _SuppliersName; }
        }
        public string SupplierVATRegNo
        {
            set {_SupplierVATRegNo = value; }
            get { return _SupplierVATRegNo; }
        }
        public string CustomerName
        {
            set { _CustomerName = value; }
            get { return _CustomerName;; }
        }
        public string InvoiceReference
        {
            set { _InvoiceReference = value; }
            get { return _InvoiceReference;; }
        }
        public string InvoiceDate
        {
            set { _InvoiceDate = value; }
            get { return _InvoiceDate; }
        }
        public string TaxPointDate
        {
            set {_TaxPointDate = value; }
            get { return _TaxPointDate; }
        }       
        public string Currency
        {
            set { _Currency = value; }
            get { return _Currency;; }
        }
        public string MatchingStatus
        {
            set { _MatchingStatus = value; }
            get { return _MatchingStatus; }
        }
        public string HeaderExtraData
        {
            set { _HeaderExtraData = value; }
            get { return _HeaderExtraData; }
        }
    }
**************
 public class Address
    {
        private string _AddressLine1;
        private string _AddressLine2;
        private string _AddressLine3;
        private string _AddressLine4;
        private string _AddressLine5;
        private string _PostCode;
 
        public string AddressLine1
        {
            set { _AddressLine1 = value; }
            get { return _AddressLine1; }
        }
        public string AddressLine2
        {
            set { _AddressLine2 = value; }
            get { return _AddressLine2; }
        }
        public string AddressLine3
        {
            set { _AddressLine3 = value; }
            get { return _AddressLine3; }
        }
        public string AddressLine4
        {
            set { _AddressLine4 = value; }
            get { return _AddressLine4; }
        }
        public string AddressLine5
        {
            set { _AddressLine5 = value; }
            get { return _AddressLine5; }
        }
        public string PostCode
        {
            set { _PostCode = value; }
            get { return _PostCode; }
        }
    }

Open in new window

0
Comment
Question by:WannabeNerd
  • 6
  • 4
11 Comments
 
LVL 6

Expert Comment

by:HarryNS
ID: 24225345
Usually, it serializes and your output will be in the same order as your properties are defined in the Class. Please check.
0
 

Author Comment

by:WannabeNerd
ID: 24225378
Yea i can understand but in the above code you see it is putting address element at first, even if supplier name is  first in class.
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24225498
but your public object for the address class are before that
any public object will also be serialized
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:WannabeNerd
ID: 24225632
So it means that i  have to make my private objects to public then ?
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24225804
no just move them down in the location you want them to be in the xml file
0
 

Author Comment

by:WannabeNerd
ID: 24225834
They are all in the proper location . But it seems that private members are being generated first then public.
So i have to made every object public to make it work accordingly.
Any suggestions?
0
 

Author Comment

by:WannabeNerd
ID: 24225899
Also if i have to  make each of them public then i might as well remove  constructors for them.
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24225939
actually the best way for doing enscapulation will be by declaring a private object and then creating a public property to access the object
in this way you can jsut keep the address(s) objects in the serialiazed xml in the place you want to be in the list of xml elements

what purpose you think will be served by removing the constructors
0
 

Author Comment

by:WannabeNerd
ID: 24225983
This is exactly what i am doing. If you check my code i have declared the private element and then public constructors. But for some reason  xml serilization  takes place first for public and then for private members even if i have encapsulated objects.

So it works fine when i use public objects and stop using constructors.Not a good ideat to as not best practice but i do not have any other option for the mom.
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 2000 total points
ID: 24226079
try this i have just made one address private and its below the supplier name
it should reder correctly in the serialized xml
public class InvoiceHeader
    {
        private string _SuppliersName;
        private Address _SuppliersAddress;
        private string _SupplierVATRegNo;
        private string _CustomerName;
        public Address  CustomerAddress;
        private string _InvoiceReference;
        private string _InvoiceDate;
        private string _TaxPointDate;
        public DeliveryDetail DeliveryDetail;
        private string _Currency;
        private string _MatchingStatus;
        private string _HeaderExtraData;
 
        public string SuppliersName
        {
            set { _SuppliersName = value; }
            get { return _SuppliersName; }
        }
	
	public Address SuppliersAddress
	{
		get { return _SuppliersAddress; }
		set { _SuppliersAddress = value; }
	}
 
        public string SupplierVATRegNo
        {
            set {_SupplierVATRegNo = value; }
            get { return _SupplierVATRegNo; }
        }
        public string CustomerName
        {
            set { _CustomerName = value; }
            get { return _CustomerName;; }
        }
        public string InvoiceReference
        {
            set { _InvoiceReference = value; }
            get { return _InvoiceReference;; }
        }
        public string InvoiceDate
        {
            set { _InvoiceDate = value; }
            get { return _InvoiceDate; }
        }
        public string TaxPointDate
        {
            set {_TaxPointDate = value; }
            get { return _TaxPointDate; }
        }       
        public string Currency
        {
            set { _Currency = value; }
            get { return _Currency;; }
        }
        public string MatchingStatus
        {
            set { _MatchingStatus = value; }
            get { return _MatchingStatus; }
        }
        public string HeaderExtraData
        {
            set { _HeaderExtraData = value; }
            get { return _HeaderExtraData; }
        }
    }

Open in new window

0
 

Author Closing Comment

by:WannabeNerd
ID: 31574229
Perfect.Thx for clearing my concept :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question