How to specify the order of XmlAttributes, using XmlSerializer
Asked Answered
W

6

11

XmlElement has an "Order" attribute which you can use to specify the precise order of your properties (in relation to each other anyway) when serializing using XmlSerializer.

public class bookingList
{
    [XmlElement(Order = 1)]
    public string error { get; set; }
    [XmlElement(Order = 2)]
    public int counter { get; set; }
    [XmlElement(ElementName = "booking", Order = 3)]
    public List<booking> bookings = new List<booking>();
}

Is there a similar thing for XmlAttribute? I just want to set the order of the attributes from something like

<MyType end="bob" start="joe" />

to

<MyType start="joe" end="bob" />

This is just for readability, my own benefit really.

Wristwatch answered 12/4, 2010 at 15:47 Comment(0)
O
12

You don't, as attributes have no order in XML (section 3.1 of the XML recommendation says: "Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.").

Overstock answered 12/4, 2010 at 15:51 Comment(3)
I take your point. However I have to disagree with that statement you quoted (I know they're not your words), the order is significant to me. Yes it will still work either way, but I do care and I do want to set the order :'(Wristwatch
As an aside, there exists so-called Canonical XML where attributes are sorted lexicographically, see w3.org/TR/xml-c14n11/#DocumentOrderPostobit
I see. What you could do is have your own implementation or wrapper for XmlWriter and then sort them as you see fit there.Overstock
B
11

From my experience, the order of serialization of attributes is the same as the order you define your public properties. However, if you combine properties with fields in the same class, e.g.

[Serializable()]
public class MyClass
{
   [XmlAttribute("ADoubleProp")]
   public double ADoubleProp { get; set; }

   [XmlAttribute("AnIntField")]
   public int AnIntField = 42;
}

then the fields get written firsts as attributes and then the properties. The code above will produce something like this

<MyClass AnIntField="42" ADoubleProp="0" />
Binns answered 23/9, 2014 at 0:25 Comment(0)
A
1

In C#, as far as what I have found, the order of attributes are serialized in the order that they are defined in the class.

See my answer to this question here: https://mcmap.net/q/394719/-how-can-i-control-the-order-of-attributes-in-my-xml-serialization

Astrid answered 30/1, 2014 at 21:50 Comment(0)
S
0

If you are creating the XML dynamically, try changing the order in which you append the attribute to the node and it should work :)

Sultry answered 13/7, 2019 at 14:42 Comment(0)
E
0

You can first serialize to an XmlDocument and then manually change the order of the attributes by removing them and then adding them again in the order you want.

While this does not make use of .NET annotations/attributes, it gives you full control over the attribute order.

I took the following code from my answer to a similar question:

public static void SortAllAttributes(XmlDocument xmlDocument)
{
    foreach (XmlElement element in xmlDocument.SelectNodes("//*"))
    {
        var attributes = element.Attributes
            .Cast<XmlAttribute>()
            .ToArray();
        
        element.Attributes.RemoveAll();

        foreach (var attribute in attributes
                        .OrderBy(a => a.LocalName) // <-- custom sort order
                        .ThenBy(a => a.Name))
        {
            element.Attributes.Append(attribute);
        }
    }
}
Epi answered 30/11, 2023 at 17:15 Comment(0)
G
-1
xmlNode.Attributes.InsertAfter(newAttribute, refAttribute); 
xmlNode.Attributes.InsertBefore(newAttribute, refAttribute);
Goar answered 11/12, 2020 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.