user3584562
user3584562

Reputation: 63

Trying to Use XDocument and LINQ

I am still learning how to use LINQ and now I'm struggling with this situation.

I have this XML file (example)

<Results>
  <PrxComissao>
    <Id>0</Id>
    <NumErro>0</NumErro>
    <RetCode>0</RetCode>
    <IdEvento>0</IdEvento>
    <ExecutionTimeMilliseconds>63596143450994.227</ExecutionTimeMilliseconds>
    <ExecutionTimeSeconds>63596143450.994225</ExecutionTimeSeconds>
    <CodComissao>CFE</CodComissao>
    <Montante>20.00</Montante>
    <Percentagem>0.0000</Percentagem>
    <MntMin>0.00</MntMin>
    <MntMax>0.00</MntMax>
    <Nome>CFE</Nome>
    <Descricao>Custo Factura Estrangeiro</Descricao>
  </PrxComissao>
  <PrxComissao>
    <Id>0</Id>
    <NumErro>0</NumErro>
    <RetCode>0</RetCode>
    <IdEvento>0</IdEvento>
    <ExecutionTimeMilliseconds>63596143450994.227</ExecutionTimeMilliseconds>
    <ExecutionTimeSeconds>63596143450.994225</ExecutionTimeSeconds>
    <CodComissao>CFE</CodComissao>
    <Montante>20.00</Montante>
    <Percentagem>0.0000</Percentagem>
    <MntMin>13.00</MntMin>
    <MntMax>123.00</MntMax>
    <Nome>CFE</Nome>
    <Descricao>www</Descricao>
  </PrxComissao>
</Results>

And now what I want to do is get all the XML elements inside the "PrxComissao", and then assign them to my class. This is the code I was trying

XDocument xDoc = XDocument.Parse(resultado);

List<PrxComissao> lstPrxComissao = xDoc.Elements("Results")
                                       .Elements("PrxComissao")
                                       .Elements()
                                       .Select(BL_CartaoCredito.Utils.Data.Converter.FromXElement<PrxComissao>)
                                       .ToList();

ObjAuxResult = lstPrxComissao;

What I am trying to do with this Converter.FromXElement<PrxComissao> is get all that elements and assign them.

Here is my class

public class PrxComissao
{

    public string CodComissao { get; set; }
    public string Montante { get; set; }
    public string Percentagem { get; set; }
    public string MntMin { get; set; }
    public string MntMax { get; set; }
    public string Nome { get; set; }
    public string Descricao { get; set; }
    public string TipoImposto { get; set; }
    public string ComFinanciamento { get; set; }
    public string iActivo { get; set; }
    public string UtlModificacao { get; set; }
    public string DtModificacao { get; set; }
    public string UtlCriacao { get; set; }
    public string DtCriacao { get; set; }
}

public static T FromXElement<T>(XElement element) where T : class, new()
{

    T value = new T();

    foreach (var subElement in element.Elements())
    {

        var field = typeof(T).GetField(subElement.Name.LocalName);
        field.SetValue(value, (string)subElement);
    }

    return value;
}

So now I have two problems. First, I can't get to the elements inside PrxComissao always returns me nothing and then is my LINQ Select correct ? Or is there a better way ?

Upvotes: 1

Views: 61

Answers (2)

har07
har07

Reputation: 89325

Currently, your code passes individual child elements of <PrxComissao> to the converter method. I believe you want to pass XElement that references <PrxComissao> instead :

List<PrxComissao> lstPrxComissao = 
                    xDoc.Elements("Results")
                        .Elements("PrxComissao")
                        .Select(o => BL_CartaoCredito.Utils
                                                     .Data
                                                     .Converter
                                                     .FromXElement<PrxComissao>(o)
                        )
                        .ToList();

Besides, your class uses properties instead of field, so the corresponding reflection method supposed to be used here is GetProperty(), not GetField().

Upvotes: 1

Crowcoder
Crowcoder

Reputation: 11514

Start with the Descendants assuming your convertor takes an XElement:

List<PrxComissao> lstPrxComissao = xDoc.Descendants()
                                       .Elements("PrxComissao")
                                       .Select(el => BL_CartaoCredito.Utils.Data.Converter.FromXElement<PrxComissao>(el))
                                       .ToList();

and then (untested) ...

public static T FromXElement<T>(XElement element) where T : class, new() 
{
    var typeOfT = typeof(T);
    T value = new T();
    foreach (var subElement in element.Elements())
    {
        var prop = typeOfT.GetProperty(subElement.Name.LocalName);
        if(prop != null)
        {
            prop.SetValue(value, subElement.Value);
        }
     }
        return value;
 }

Upvotes: 1

Related Questions