I’m in need to serialize an string that is valid xml to an object. The thing is that object is 2 arrays of other objects, and i don’t know if the following annotation is correct:
[XmlRoot("Data")]
public class Data
{
[XmlArray("ServicoUrgente")]
public ServicoUrgente[] servicosUrgentes;
[XmlArray("Mensagem")]
public Mensagem[] mensagens;
}
[XmlRoot("Mensagem")]
public class Mensagem
{
[XmlElement("ToUserID")]
public int ToUserID;
[XmlElement("Message")]
public int Message;
[XmlElement("TimeStamp")]
public DateTime TimeStamp;
}
[XmlRoot("ServicoUrgente")]
public class ServicoUrgente
{
[XmlElement("ServicoID")]
public int ServicoID;
[XmlElement("ToUserID")]
public int ToUserID;
[XmlElement("FromUserID")]
public int FromUserID;
[XmlElement("FromUserName")]
public string FromUserName;
[XmlElement("DataInicioPrevista")]
public DateTime DataInicioPrevista;
[XmlElement("DataFimPrevista")]
public DateTime DataFimPrevista;
[XmlElement("IDPoi")]
public int IDPoi;
[XmlElement("NomePoi")]
public string Nome;
[XmlElement("DescricaoPoi")]
public string Descricao;
[XmlElement("NContratoPoi")]
public string NContrato;
[XmlElement("MoradaPoi")]
public string Morada;
[XmlElement("LatitudePoi")]
public double Latitude;
[XmlElement("LongitudePoi")]
public double Longitude;
}
The questions is: DO we have for each class an xmlRoot attribute, or only the Data class has it, and the other two have another attribute instead of the xmlroot
XmlRootis only going to do anything if the object ends up as the root element of a serialized file. It does not affect how objects are named inside arrays (as far as i know).To set the names of arrays and the objects inside use the respective attributes:
We cannot know if your code is correct if you don’t post the XML it should translate to or read from.