I have a REST service returning XML of a serialized object containing a list of int. The object code is below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace All.Tms.Dto
{
[XmlRoot(Namespace = "http://schemas.datacontract.org/2004/07/All.Tms.Dto")]
public class ReadSensorsForVehicleIdResponse
{
public List<int> sensorIdList { get; set; }
}
}
When this object is serialized the XML is generated and sent as:
<ReadSensorsForVehicleIdResponse xmlns="http://schemas.datacontract.org/2004/07/All.Tms.Dto" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><sensorIdList xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><a:int>107</a:int></sensorIdList></ReadSensorsForVehicleIdResponse>
The problem is that the int values are serialized as
<a:int>107</a:int>
this causes the deserializing of the object to fail. When I change
<a:int>107</a:int>
to
<int>107</int>
the object deserialize correctly. Is there any reason why the int values are serialized this way and how can I fix this problem?
Here is the code I use to deserialize
public static T Deserialize<T>(string xml) where T : class
{
var serializer = new XmlSerializer(typeof(T));
var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
var reader = XmlReader.Create(stream);
return (T)serializer.Deserialize(reader);
}
Linq To Xml is easy to use