Reputation: 122
So, I've been trying to deserialize this XML:
<DCR>
<Xcore_PD12M_ALL_PC_NC_Global>
<Xcore>411</Xcore>
<Categoria_de_Riesgo>Risk Alert</Categoria_de_Riesgo>
<Probabilidad_de_incumplimiento>91.75</Probabilidad_de_incumplimiento>
<HorizonteTiempo>
<Desde>02.2018</Desde>
<Hasta>01.2019</Hasta>
</HorizonteTiempo>
<Odds>
<Odds_A_Favor>1</Odds_A_Favor>
<Odds_EnContra>11.1</Odds_EnContra>
<ObligorAzar>12.1</ObligorAzar>
<ObligorIncumplimiento>11.1</ObligorIncumplimiento>
<PorcientoDeudorIncumplimiento>97.61</PorcientoDeudorIncumplimiento>
<PorcientoDeudorNoIncumplimiento>2.39</PorcientoDeudorNoIncumplimiento>
<QuantilPoblacion>29.89</QuantilPoblacion>
</Odds>
<Mes_evaluacion>01.2018</Mes_evaluacion>
</Xcore_PD12M_ALL_PC_NC_Global>
<DeudasPorProducto>
<Producto Nombre="Tarjeta de Crédito">
<EstatusImg>CR</EstatusImg>
<EstatusImg2 />
<CantidadCuentas>2</CantidadCuentas>
<CreditoAprobado Moneda="R.D. $">15000</CreditoAprobado>
<TotalAdeudado>23488</TotalAdeudado>
<Cuota>2194</Cuota>
<EnAtraso />
<EnLegal />
<Castigado>17504</Castigado>
</Producto>
<Producto Nombre="Tarjeta de Crédito">
<EstatusImg>CN</EstatusImg>
<EstatusImg2 />
<CantidadCuentas>1</CantidadCuentas>
<CreditoAprobado Moneda="U.S. $">219</CreditoAprobado>
<TotalAdeudado />
<Cuota />
<EnAtraso />
<EnLegal />
<Castigado />
</Producto>
<Producto Nombre="SERVICIOS">
<EstatusImg>CR</EstatusImg>
<EstatusImg2 />
<CantidadCuentas>1</CantidadCuentas>
<CreditoAprobado Moneda="R.D. $">2534</CreditoAprobado>
<TotalAdeudado>2534</TotalAdeudado>
<Cuota />
<EnAtraso />
<EnLegal />
<Castigado>2534</Castigado>
</Producto>
<Producto Nombre="Producto de Telecomunicaciones">
<EstatusImg>CG</EstatusImg>
<EstatusImg2 />
<CantidadCuentas>3</CantidadCuentas>
<CreditoAprobado Moneda="R.D. $">3021</CreditoAprobado>
<TotalAdeudado>1979</TotalAdeudado>
<Cuota />
<EnAtraso />
<EnLegal />
<Castigado />
</Producto>
<Producto Nombre="Préstamo">
<EstatusImg>CN</EstatusImg>
<EstatusImg2 />
<CantidadCuentas>2</CantidadCuentas>
<CreditoAprobado Moneda="R.D. $">40000</CreditoAprobado>
<TotalAdeudado />
<Cuota />
<EnAtraso />
<EnLegal />
<Castigado />
</Producto>
<Total ID="R.D. $ PESO">
<Total_CreditoAprobado>$60,555</Total_CreditoAprobado>
<Total_Adeudado>$28,001</Total_Adeudado>
<Total_Cuota>$2,194</Total_Cuota>
<Total_EnAtraso />
<Total_EnLegal />
<Total_Castigado>$20,038</Total_Castigado>
</Total>
<Total ID="U.S. $ DOLAR">
<Total_CreditoAprobado>$219</Total_CreditoAprobado>
<Total_Adeudado />
<Total_Cuota />
<Total_EnAtraso />
<Total_EnLegal />
<Total_Castigado />
</Total>
</DeudasPorProducto>
</DCR>
And having trouble deserializing this section:
<DeudasPorProducto>
<Producto Nombre="Tarjeta de Crédito">
<EstatusImg>CR</EstatusImg>
<EstatusImg2 />
<CantidadCuentas>2</CantidadCuentas>
<CreditoAprobado Moneda="R.D. $">15000</CreditoAprobado>
<TotalAdeudado>23488</TotalAdeudado>
<Cuota>2194</Cuota>
<EnAtraso />
<EnLegal />
<Castigado>17504</Castigado>
</Producto>
<Producto Nombre="Tarjeta de Crédito">
<EstatusImg>CN</EstatusImg>
<EstatusImg2 />
<CantidadCuentas>1</CantidadCuentas>
<CreditoAprobado Moneda="U.S. $">219</CreditoAprobado>
<TotalAdeudado />
<Cuota />
<EnAtraso />
<EnLegal />
<Castigado />
</Producto>
<Producto Nombre="SERVICIOS">
<EstatusImg>CR</EstatusImg>
<EstatusImg2 />
<CantidadCuentas>1</CantidadCuentas>
<CreditoAprobado Moneda="R.D. $">2534</CreditoAprobado>
<TotalAdeudado>2534</TotalAdeudado>
<Cuota />
<EnAtraso />
<EnLegal />
<Castigado>2534</Castigado>
</Producto>
<Producto Nombre="Producto de Telecomunicaciones">
<EstatusImg>CG</EstatusImg>
<EstatusImg2 />
<CantidadCuentas>3</CantidadCuentas>
<CreditoAprobado Moneda="R.D. $">3021</CreditoAprobado>
<TotalAdeudado>1979</TotalAdeudado>
<Cuota />
<EnAtraso />
<EnLegal />
<Castigado />
</Producto>
<Producto Nombre="Préstamo">
<EstatusImg>CN</EstatusImg>
<EstatusImg2 />
<CantidadCuentas>2</CantidadCuentas>
<CreditoAprobado Moneda="R.D. $">40000</CreditoAprobado>
<TotalAdeudado />
<Cuota />
<EnAtraso />
<EnLegal />
<Castigado />
</Producto>
<Total ID="R.D. $ PESO">
<Total_CreditoAprobado>$60,555</Total_CreditoAprobado>
<Total_Adeudado>$28,001</Total_Adeudado>
<Total_Cuota>$2,194</Total_Cuota>
<Total_EnAtraso />
<Total_EnLegal />
<Total_Castigado>$20,038</Total_Castigado>
</Total>
<Total ID="U.S. $ DOLAR">
<Total_CreditoAprobado>$219</Total_CreditoAprobado>
<Total_Adeudado />
<Total_Cuota />
<Total_EnAtraso />
<Total_EnLegal />
<Total_Castigado />
</Total>
</DeudasPorProducto>
As you can see, the Element "Producto" is mixed up with another element called "Total", which may be why it isn't deserializing. These are the classes I'm using:
public class DeudasPorProducto
{
[XmlElement("Producto")]
public List<Producto> Productos { get; set; }
}
public class Xcore
{
[XmlElement("Xcore")]
public int CrediScore { get; set; }
[XmlElement("Categoria_de_Riesgo")]
public string CategoriaRiesgo { get; set; }
[XmlElement("Probabilidad_de_incumplimiento")]
public double ProbabilidadIncumplimiento { get; set; }
[XmlElement("HorizonteTiempo")]
public Timeline HorizonteTiempo { get; set; }
[XmlElement("Odds")]
public Odds Odds { get; set; }
[XmlElement("Mes_evaluacion")]
public string MesEvaluacion { get; set; }
[XmlElement("DeudasPorProducto")]
public DeudasPorProducto Deudas { get; set; }
}
[XmlRoot("DCR")]
public class DataCreditoCreditReport
{
[XmlElement("Xcore_PD12M_ALL_PC_NC_Global")]
public Xcore Xcore { get; set; }
}
The last thing I tried was using the Producto Class as a list as property on Xcore:
[XmlArray("DeudasPorProducto")]
[XmlArrayItem("Producto")]
public List<Producto> Productos { get; set; }
But it didn't take. I need a helping hand at this point. :)
Upvotes: 0
Views: 75
Reputation: 2714
You can use Xml2CSharp web service to generate classes with necessary attributes.
The service returns C# classes which you can rename with your coding style.
I have checked generated classes and it works fine.
My fiddler is here: .NET Fiddler
You need to use next declaration for Producte class:
[XmlRoot(ElementName="DeudasPorProducto")]
public class DeudasPorProducto {
[XmlElement(ElementName="Producto")]
public List<Producto> Producto { get; set; }
}
Upvotes: 1
Reputation: 4802
If you copy the XML example in your question and paste it as a class using Visual Studio (Edit Menu -> Paste Special -> Paste XML as Classes) it will give you about 900 line boilerplate code (much of which is extremely redundant), but that will deserialize without problems and should give you a decent starting point.
var xmlSerializer = new XmlSerializer(typeof(XmlClasses.DCR));
using (var fileStream = new FileStream(@"TestXml.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
var dcr = (XmlClasses.DCR)xmlSerializer.Deserialize(fileStream);
}
You should be able to then start tweaking property names using the XmlElement attribute.
public class XmlClasses
{
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class DCR
{
private DCRXcore_PD12M_ALL_PC_NC_Global xcore_PD12M_ALL_PC_NC_GlobalField;
private DCRDeudasPorProducto deudasPorProductoField;
/// <remarks/>
public DCRXcore_PD12M_ALL_PC_NC_Global Xcore_PD12M_ALL_PC_NC_Global
{
get
{
return this.xcore_PD12M_ALL_PC_NC_GlobalField;
}
set
{
this.xcore_PD12M_ALL_PC_NC_GlobalField = value;
}
}
/// <remarks/>
public DCRDeudasPorProducto DeudasPorProducto
{
get
{
return this.deudasPorProductoField;
}
set
{
this.deudasPorProductoField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DCRXcore_PD12M_ALL_PC_NC_Global
{
private ushort xcoreField;
private string categoria_de_RiesgoField;
private decimal probabilidad_de_incumplimientoField;
private DCRXcore_PD12M_ALL_PC_NC_GlobalHorizonteTiempo horizonteTiempoField;
private DCRXcore_PD12M_ALL_PC_NC_GlobalOdds oddsField;
private decimal mes_evaluacionField;
/// <remarks/>
public ushort Xcore
{
get
{
return this.xcoreField;
}
set
{
this.xcoreField = value;
}
}
/// <remarks/>
public string Categoria_de_Riesgo
{
get
{
return this.categoria_de_RiesgoField;
}
set
{
this.categoria_de_RiesgoField = value;
}
}
/// <remarks/>
public decimal Probabilidad_de_incumplimiento
{
get
{
return this.probabilidad_de_incumplimientoField;
}
set
{
this.probabilidad_de_incumplimientoField = value;
}
}
/// <remarks/>
public DCRXcore_PD12M_ALL_PC_NC_GlobalHorizonteTiempo HorizonteTiempo
{
get
{
return this.horizonteTiempoField;
}
set
{
this.horizonteTiempoField = value;
}
}
/// <remarks/>
public DCRXcore_PD12M_ALL_PC_NC_GlobalOdds Odds
{
get
{
return this.oddsField;
}
set
{
this.oddsField = value;
}
}
/// <remarks/>
public decimal Mes_evaluacion
{
get
{
return this.mes_evaluacionField;
}
set
{
this.mes_evaluacionField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DCRXcore_PD12M_ALL_PC_NC_GlobalHorizonteTiempo
{
private decimal desdeField;
private decimal hastaField;
/// <remarks/>
public decimal Desde
{
get
{
return this.desdeField;
}
set
{
this.desdeField = value;
}
}
/// <remarks/>
public decimal Hasta
{
get
{
return this.hastaField;
}
set
{
this.hastaField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DCRXcore_PD12M_ALL_PC_NC_GlobalOdds
{
private byte odds_A_FavorField;
private decimal odds_EnContraField;
private decimal obligorAzarField;
private decimal obligorIncumplimientoField;
private decimal porcientoDeudorIncumplimientoField;
private decimal porcientoDeudorNoIncumplimientoField;
private decimal quantilPoblacionField;
/// <remarks/>
public byte Odds_A_Favor
{
get
{
return this.odds_A_FavorField;
}
set
{
this.odds_A_FavorField = value;
}
}
/// <remarks/>
public decimal Odds_EnContra
{
get
{
return this.odds_EnContraField;
}
set
{
this.odds_EnContraField = value;
}
}
/// <remarks/>
public decimal ObligorAzar
{
get
{
return this.obligorAzarField;
}
set
{
this.obligorAzarField = value;
}
}
/// <remarks/>
public decimal ObligorIncumplimiento
{
get
{
return this.obligorIncumplimientoField;
}
set
{
this.obligorIncumplimientoField = value;
}
}
/// <remarks/>
public decimal PorcientoDeudorIncumplimiento
{
get
{
return this.porcientoDeudorIncumplimientoField;
}
set
{
this.porcientoDeudorIncumplimientoField = value;
}
}
/// <remarks/>
public decimal PorcientoDeudorNoIncumplimiento
{
get
{
return this.porcientoDeudorNoIncumplimientoField;
}
set
{
this.porcientoDeudorNoIncumplimientoField = value;
}
}
/// <remarks/>
public decimal QuantilPoblacion
{
get
{
return this.quantilPoblacionField;
}
set
{
this.quantilPoblacionField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DCRDeudasPorProducto
{
private DCRDeudasPorProductoProducto[] productoField;
private DCRDeudasPorProductoTotal[] totalField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Producto")]
public DCRDeudasPorProductoProducto[] Producto
{
get
{
return this.productoField;
}
set
{
this.productoField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Total")]
public DCRDeudasPorProductoTotal[] Total
{
get
{
return this.totalField;
}
set
{
this.totalField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DCRDeudasPorProductoProducto
{
private string estatusImgField;
private object estatusImg2Field;
private byte cantidadCuentasField;
private DCRDeudasPorProductoProductoCreditoAprobado creditoAprobadoField;
private string totalAdeudadoField;
private string cuotaField;
private object enAtrasoField;
private object enLegalField;
private string castigadoField;
private string nombreField;
/// <remarks/>
public string EstatusImg
{
get
{
return this.estatusImgField;
}
set
{
this.estatusImgField = value;
}
}
/// <remarks/>
public object EstatusImg2
{
get
{
return this.estatusImg2Field;
}
set
{
this.estatusImg2Field = value;
}
}
/// <remarks/>
public byte CantidadCuentas
{
get
{
return this.cantidadCuentasField;
}
set
{
this.cantidadCuentasField = value;
}
}
/// <remarks/>
public DCRDeudasPorProductoProductoCreditoAprobado CreditoAprobado
{
get
{
return this.creditoAprobadoField;
}
set
{
this.creditoAprobadoField = value;
}
}
/// <remarks/>
public string TotalAdeudado
{
get
{
return this.totalAdeudadoField;
}
set
{
this.totalAdeudadoField = value;
}
}
/// <remarks/>
public string Cuota
{
get
{
return this.cuotaField;
}
set
{
this.cuotaField = value;
}
}
/// <remarks/>
public object EnAtraso
{
get
{
return this.enAtrasoField;
}
set
{
this.enAtrasoField = value;
}
}
/// <remarks/>
public object EnLegal
{
get
{
return this.enLegalField;
}
set
{
this.enLegalField = value;
}
}
/// <remarks/>
public string Castigado
{
get
{
return this.castigadoField;
}
set
{
this.castigadoField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Nombre
{
get
{
return this.nombreField;
}
set
{
this.nombreField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DCRDeudasPorProductoProductoCreditoAprobado
{
private string monedaField;
private ushort valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Moneda
{
get
{
return this.monedaField;
}
set
{
this.monedaField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public ushort Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DCRDeudasPorProductoTotal
{
private string total_CreditoAprobadoField;
private string total_AdeudadoField;
private string total_CuotaField;
private object total_EnAtrasoField;
private object total_EnLegalField;
private string total_CastigadoField;
private string idField;
/// <remarks/>
public string Total_CreditoAprobado
{
get
{
return this.total_CreditoAprobadoField;
}
set
{
this.total_CreditoAprobadoField = value;
}
}
/// <remarks/>
public string Total_Adeudado
{
get
{
return this.total_AdeudadoField;
}
set
{
this.total_AdeudadoField = value;
}
}
/// <remarks/>
public string Total_Cuota
{
get
{
return this.total_CuotaField;
}
set
{
this.total_CuotaField = value;
}
}
/// <remarks/>
public object Total_EnAtraso
{
get
{
return this.total_EnAtrasoField;
}
set
{
this.total_EnAtrasoField = value;
}
}
/// <remarks/>
public object Total_EnLegal
{
get
{
return this.total_EnLegalField;
}
set
{
this.total_EnLegalField = value;
}
}
/// <remarks/>
public string Total_Castigado
{
get
{
return this.total_CastigadoField;
}
set
{
this.total_CastigadoField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ID
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class DeudasPorProducto
{
private DeudasPorProductoProducto[] productoField;
private DeudasPorProductoTotal[] totalField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Producto")]
public DeudasPorProductoProducto[] Producto
{
get
{
return this.productoField;
}
set
{
this.productoField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Total")]
public DeudasPorProductoTotal[] Total
{
get
{
return this.totalField;
}
set
{
this.totalField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DeudasPorProductoProducto
{
private string estatusImgField;
private object estatusImg2Field;
private byte cantidadCuentasField;
private DeudasPorProductoProductoCreditoAprobado creditoAprobadoField;
private string totalAdeudadoField;
private string cuotaField;
private object enAtrasoField;
private object enLegalField;
private string castigadoField;
private string nombreField;
/// <remarks/>
public string EstatusImg
{
get
{
return this.estatusImgField;
}
set
{
this.estatusImgField = value;
}
}
/// <remarks/>
public object EstatusImg2
{
get
{
return this.estatusImg2Field;
}
set
{
this.estatusImg2Field = value;
}
}
/// <remarks/>
public byte CantidadCuentas
{
get
{
return this.cantidadCuentasField;
}
set
{
this.cantidadCuentasField = value;
}
}
/// <remarks/>
public DeudasPorProductoProductoCreditoAprobado CreditoAprobado
{
get
{
return this.creditoAprobadoField;
}
set
{
this.creditoAprobadoField = value;
}
}
/// <remarks/>
public string TotalAdeudado
{
get
{
return this.totalAdeudadoField;
}
set
{
this.totalAdeudadoField = value;
}
}
/// <remarks/>
public string Cuota
{
get
{
return this.cuotaField;
}
set
{
this.cuotaField = value;
}
}
/// <remarks/>
public object EnAtraso
{
get
{
return this.enAtrasoField;
}
set
{
this.enAtrasoField = value;
}
}
/// <remarks/>
public object EnLegal
{
get
{
return this.enLegalField;
}
set
{
this.enLegalField = value;
}
}
/// <remarks/>
public string Castigado
{
get
{
return this.castigadoField;
}
set
{
this.castigadoField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Nombre
{
get
{
return this.nombreField;
}
set
{
this.nombreField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DeudasPorProductoProductoCreditoAprobado
{
private string monedaField;
private ushort valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Moneda
{
get
{
return this.monedaField;
}
set
{
this.monedaField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public ushort Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DeudasPorProductoTotal
{
private string total_CreditoAprobadoField;
private string total_AdeudadoField;
private string total_CuotaField;
private object total_EnAtrasoField;
private object total_EnLegalField;
private string total_CastigadoField;
private string idField;
/// <remarks/>
public string Total_CreditoAprobado
{
get
{
return this.total_CreditoAprobadoField;
}
set
{
this.total_CreditoAprobadoField = value;
}
}
/// <remarks/>
public string Total_Adeudado
{
get
{
return this.total_AdeudadoField;
}
set
{
this.total_AdeudadoField = value;
}
}
/// <remarks/>
public string Total_Cuota
{
get
{
return this.total_CuotaField;
}
set
{
this.total_CuotaField = value;
}
}
/// <remarks/>
public object Total_EnAtraso
{
get
{
return this.total_EnAtrasoField;
}
set
{
this.total_EnAtrasoField = value;
}
}
/// <remarks/>
public object Total_EnLegal
{
get
{
return this.total_EnLegalField;
}
set
{
this.total_EnLegalField = value;
}
}
/// <remarks/>
public string Total_Castigado
{
get
{
return this.total_CastigadoField;
}
set
{
this.total_CastigadoField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ID
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
}
Upvotes: 1