• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1028
  • Last Modified:

How to serialize XML attributes in a certain order in C#?

I'm using XML serialization to write an object to a xml file.  I'm using the following code:

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add( "xsi",  "www.someotherstuff.com");
namespaces.Add( "xyz", "www.mystuff.com" );

writer = new StreamWriter(filename);
XmlSerializer serializer = new XmlSerializer(myObject.GetType());                        

serializer.Serialize(writer, myObject, namespaces);

My question is this:  I have an attribute (myAttribute1) for my object which I want to appear in a specific order -  before the namespaces like the following:

<?xml version="1.0" encoding="UTF-8"?>
<xyz:MyObject myAttribute1="123" xmlns:xyz="www.mystuff.com" xmlns:xsi="www.someotherstuff.com">

However, when I serialize the object, I get the following:
<xyz:MyObject  xmlns:xyz="www.mystuff.com" myAttribute1="123" xmlns:xsi="www.someotherstuff.com">

Is there any way to force the attribute to appear first, before the namespaces?



1 Solution
No I do not believe there is without writing a custom serializer.

Of course the real problem here is that attribute order is supposed to be insignificant ... By adding this requirement you may be making you document not compatible.



Featured Post

Technology Partners: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now