Jmperez Jmperez - 3 months ago 19
C# Question

How to change the order properties are serialized in C#

I have a list in C# with 4 items. This list is used to send a response in a web service and I need a specific order for the items, but I'm having a problem because, for some reason, the list changes the order when I fill it.

First, this is the class of the list

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Mvm.SATWeb.Domain
{
[Serializable, DataContract]
public class puntoDeAtencion
{

public puntoDeAtencion()
{
}

[DataMember]
public string codigoPuntoAtencion { get; set; }
[DataMember]
public decimal montoIngreso { get; set; }
[DataMember]
public decimal montoEgreso { get; set; }
[DataMember]
public decimal ingresoNeto { get; set; }

}
}


I use a SQL server query to fill the list with a dataset

List<puntoDeAtencion> valores = new List<puntoDeAtencion>();
DataSet ds;

Database baseDatos = DatabaseFactory.CreateDatabase();
DbCommand comandoConsulta = baseDatos.GetStoredProcCommand("USP_RiesgoLiqui");
comandoConsulta.CommandTimeout = 600000;
baseDatos.AddInParameter(comandoConsulta, "@pvstrIdAgencia", DbType.String, "-1");
baseDatos.AddInParameter(comandoConsulta, "@pvstrFechaInicial", DbType.String, FechaIni);
baseDatos.AddInParameter(comandoConsulta, "@pvstrFechaFinal", DbType.String, FechaFin);

comandoConsulta.CommandTimeout = 1000000;
// baseDatos.ExecuteDataSet();

ds = baseDatos.ExecuteDataSet(comandoConsulta);

if (ds.Tables.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count ; i++)
{
// valores.Add(s)



//valores.Add (new punptoDeAtencion(){}
valores.Add(new puntoDeAtencion() { codigoPuntoAtencion = Convert.ToString(ds.Tables[0].Rows[i]["Agencia"]), montoIngreso = Convert.ToDecimal(ds.Tables[0].Rows[i]["INGRESONETO"]), montoEgreso = Convert.ToDecimal(ds.Tables[0].Rows[i]["MONTOEGRESO"]), ingresoNeto = Convert.ToDecimal(0.00) });


// var list1 = (from p in ds.Tables[0].Rows[i] select p).ToList();
}
}


return valores.ToList<puntoDeAtencion>();


This is the response (Using SOAP UI, but when I debug show the same values in the response object)

<b:listaPuntosDeAtencion>
<b:puntoDeAtencion>
<b:codigoPuntoAtencion>001</b:codigoPuntoAtencion>
<b:ingresoNeto>0</b:ingresoNeto>
<b:montoEgreso>53266155.0000</b:montoEgreso>
<b:montoIngreso>138285187.0000</b:montoIngreso>
</b:puntoDeAtencion>


and this is how it should be

<listaPuntosDeAtencion>
<puntoDeAtencion>
<codigoPuntoAtencion>00654</codigoPuntoAtencion>
<montoIngreso>79000.0</montoIngreso>
<montoEgreso>30000.0</montoEgreso>
<ingresoNeto>0.0</ingresoNeto>
</puntoDeAtencion>


I want to order the list or the response, I don't know if LINQ works in this case.

Answer

You can order them by using Order on DataMember like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Mvm.SATWeb.Domain
{
    [Serializable, DataContract]
    public  class puntoDeAtencion
    {

        public puntoDeAtencion()
        {
        }

        [DataMember(Order = 0)]
        public string codigoPuntoAtencion { get; set; }
        [DataMember(Order = 1)]
        public decimal montoIngreso { get; set; }
        [DataMember(Order = 2)]
        public decimal montoEgreso { get; set; }
        [DataMember(Order = 3)]
        public decimal ingresoNeto { get; set; }         

   }
}

Documentation here: https://msdn.microsoft.com/en-us/library/ms729813.aspx