Title: net xmlserializer : keeping attributes order
Tags: c#;.net;xml-serialization
Question: I can sucessfully able to write my  class Item in a xml file. But the orders of atttributes change...
Suppose I have Item class
class Item
Name
Price
when I write it to xml file using .net xmlserializer  I get different order from my class decleration such as
<Item Price="y" Name="x"  Id="z"  /&gt;
But I want it like this [ keep decleration order]
<Item Name="x" Price="y" Id="z"  /&gt;
How can İ do it in  NET?
Comment: This might be a dumb question, but since this is xml and you should be parsing it with an xml library.... why does it matter?

Comment: I do not depend on order guys...I just want some cosmetic good looking on my xml...And I know that XML is not for human reading...But i am mad about "form" and I want it in some order...


Here is another answer: You you are keen on the order of the attributes, then the IXmlSerializable interface will give you  control of the serialization/deserialization process of the class. The order of attributes is determined by the order of the lines of code:
public void WriteXml(XmlWriter writer)
//First example xml element
writer.WriteStartElement("Item1");
writer.WriteAttributeString("Name", Name);
writer.WriteAttributeString("Price", Price);
writer.WriteAttributeString("Id", Id);
writer.WriteEndElement();
//Second example xml element
writer.WriteStartElement("Item2");
writer.WriteAttributeString("Price", Price);
writer.WriteAttributeString("Id", Id);
writer.WriteAttributeString("Name", Name);
writer.WriteEndElement();
outputs:
<Item1 Name="x" Price="y" Id="z"&gt;
<Item2 Price="y" Id="z" Name="x"&gt;
As you see, if you switch around lines of code, then the order is changed.
But beware, implementing this interface overwrites the default process, letting it be up to you to write the entire serialization/deserialization process.
Regards
Nautious

Here is another answer: You shouldn't be concerend with the order. If you are, then you are not processing your xml properly
section 3.1 "Note that the order of attribute specifications in a
start-tag or empty-element tag is not significant."
Comment for this answer: I personally wouldn't bother, like you said it's not for human reading. But if you really want to I think this article may deal with something similar http://blogs.microsoft.co.il/blogs/rotemb/archive/2008/07/27/customize-object-xml-serialization-with-net-xml-attributes.aspx
Comment for this answer: I know  that XML is not for human reading...It may be stupid but I want the order just because of some "cosmetic" good looking...That is all...

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:07/12/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><.net><xml-serialization>', 'Title': 'net xmlserializer : keeping attributes order', 'ViewCount': 1364, 'id': 11452293, 'user_AboutMe': '<p>It is just me:-)</p>\n', 'user_AccountId': 408307.0, 'user_DisplayName': 'Novalis', 'user_Reputation': 2203.0, 'user_Views': 362.0, 'user_id': 778894.0}}