It is complicating me convert an XML to an object in C#.
I want to convert an XML consisting of a list of objects 'Regla' with a series of fields (idRegla, DateFrom, DateTo, and a list of exceptions that may not appear).
I'm going crazy, I do not think it's that hard ...
Here is the XML:
<ListaReglas> <REGLA> <idRegla>2</idRegla> <DateFrom>2013-12-01T00:00:00</DateFrom> <DateTo>2015-07-25T00:00:00</DateTo> <Excepciones> <FECHA>2013-12-25T00:00:00</FECHA> </Excepciones> </REGLA> <REGLA> <idRegla>4</idRegla> <DateFrom>2013-12-01T00:00:00</DateFrom> <DateTo>2015-07-25T00:00:00</DateTo> <Excepciones> <FECHA>2013-12-25T00:00:00</FECHA> </Excepciones> </REGLA> <REGLA> <idRegla>5</idRegla> <DateFrom>2013-12-01T00:00:00</DateFrom> <DateTo>2015-07-25T00:00:00</DateTo> <Excepciones> <FECHA>2013-12-25T00:00:00</FECHA> </Excepciones> </REGLA> <REGLA> <idRegla>7</idRegla> <DateFrom>2013-11-19T00:00:00</DateFrom> <DateTo>2015-12-19T00:00:00</DateTo> </REGLA> </ListaReglas> Here is my class:
[Serializable] [XmlTypeAttribute(AnonymousType = true)] public class ReglaRangoResult { [XmlElement(ElementName = "idRegla", IsNullable = false)] public int idRegla { get; set; } [XmlElement(ElementName = "DateFrom", IsNullable = false)] public DateTime DateFrom { get; set; } [XmlElement(ElementName = "DateTo", IsNullable = false)] public DateTime DateTo { get; set; } [XmlElement(ElementName = "Excepciones", IsNullable = true)] public List<DateTime> Excepciones { get; set; } [XmlIgnore] public int Peso { get; set; } } And this is my code:
[...] List<ReglaRangoResult> listaReglas = new List<ReglaRangoResult>(); XmlDoc xmlDoc = new XmlDoc(rdr.GetString(0)); foreach (XmlNode xmlNode in xmlDoc.SelectNodes("//ListaReglas/REGLA")) { listaReglas.Add(XmlToObject<ReglaRangoResult>(xmlNode.OuterXml)); } [...] public static T XmlToObject<T>(string xml) { using (var xmlStream = new StringReader(xml)) { var serializer = new XmlSerializer(typeof(T)); return (T)serializer.Deserialize(XmlReader.Create(xmlStream)); } } I don't understand what I'm doing wrong. Is the ReglaRangoResult misconfigured class? What is missing? What is left?
EXCEPTION RETURNED:
'Error reflecting type 'dllReglasNegocioMP.ReglaRangoResult'