Sorted xml element while serialising
-
I am using c# serialisation to write some xml. For some reason the output is not necessarily written in the order it appears in the code. How can I order the elements? For example:
[Serializable]
public class Request
{
[XmlElement("Type")]
public string Type
{
get;
set;
}[XmlElement("Id")]
public int Id
{
get;
set;
}
}Would write
<Request>
<Id>1</Id>
<Type>Get</Type>
</Request>(Note this example is very simplified and doesn't actually write it wrongly).
-
I am using c# serialisation to write some xml. For some reason the output is not necessarily written in the order it appears in the code. How can I order the elements? For example:
[Serializable]
public class Request
{
[XmlElement("Type")]
public string Type
{
get;
set;
}[XmlElement("Id")]
public int Id
{
get;
set;
}
}Would write
<Request>
<Id>1</Id>
<Type>Get</Type>
</Request>(Note this example is very simplified and doesn't actually write it wrongly).
Fixed it. It turns out, fields are serialised before properties and my code was actually like this:
[Serializable]
public class Request
{
[XmlElement("Type")]
public string Type
{
get;
set;
}[XmlElement("Id")]
public int Id; // <<<--- Note this is a field, not a property
}