Serialization of lists in C # classes

3

Good afternoon guys, I'm doing an NFe project to issue, so I opted, after researching using class serialization to mount the xml. Everything works fine, from a class structure that I set xml amount, but in a certain part I needed to implement lists in a part of the class to serialize, in the NFe tag det , where I add the items of the note, I perfectly mounted the class, it worked, the problem is that the tag as in the code below is not a list, and I do not know how to instantiate it as a list, in my attempts it did not work, however its childs are all ready. >

The tag has to be mounted dynamically, eg:

< det nItem="1" > ...... other tags ..... < / det > < det nItem="2" >  ...... other tags ..... < / det > and so it goes. **

public class NFe
{
    public infNFe infNFe; 
}

public class infNFe
{

    public det det; ----------FORMA NORMAL

    public List<det> det = new List<det>(); ---------TENTEI INSTANCIAR EM LISTA

}

public class det ----------------------- ELEMENTOS ABAIXO ESTÃO EM FORMA DE LISTA
{

    [XmlAttribute("nItem")]
    public string nItem { get; set; }
    // [XmlElement(Order = 1)]
    [XmlElement("prod")]
    public List<prod> prod = new List<prod>();
    //  [XmlElement(Order = 2)]
    [XmlElement("imposto")]
    public imposto imposto;
    //  [XmlElement(Order = 3)]
    [XmlElement("infAdProd")]
    public string infAdProd { get; set; }
}

public class prod
{
    public string cProd { get; set; }
    public string cEAN { get; set; }
    public string xProd { get; set; }
    public string NCM { get; set; }
    public string NVE { get; set; }
    public string EXTIPI { get; set; }
    public string CFOP { get; set; }
    public string uCom { get; set; }
    public string qCom { get; set; }
    public string vUnCom { get; set; }
    public string vProd { get; set; }
    public string cEANTrib { get; set; }
    public string uTrib { get; set; }
    public string qTrib { get; set; }
    public string vUnTrib { get; set; }
    public string vFrete { get; set; }
    public string vSeg { get; set; }
    public string vDesc { get; set; }
    public string vOutro { get; set; }
    public string indTot { get; set; }

    public prod() { }

    public prod(string cprod,
                string cean,
                string xprod,
                string ncm,
                string nve,
                string extipi,
                string cfop,
                string ucom,
                string qcom,
                string vuncom,
                string vprod,
                string ceantrib,
                string utrib,
                string qtrib,
                string vuntrib,
                string vfrete,
                string vseg,
                string vdesc,
                string voutro,
                string indtot)
    {

        cProd = cprod;
        cEAN = cean;
        xProd = xprod;
        NCM = ncm;
        NVE = nve;
        EXTIPI = extipi;
        CFOP = cfop;
        uCom = ucom;
        qCom = qcom;
        vUnCom = vuncom;
        vProd = vprod;
        cEANTrib = ceantrib;
        uTrib = utrib;
        qTrib = qtrib;
        vUnTrib = vuntrib;
        vFrete = vfrete;
        vSeg = vSeg;
        vDesc = vdesc;
        vOutro = voutro;
        indTot = indtot;

    }

}

public class imposto
{
    //    [XmlElement(Order = 1)]
    public string vTotTrib;
    //  [XmlElement(Order = 2)]
    public ICMS ICSM;
    //   [XmlElement(Order = 3)]
    public IPI IPI;
    //   [XmlElement(Order = 4)]
    public PIS PIS;
    //   [XmlElement(Order = 5)]
    public COFINS COFINS;



}

public class ICMS
{
    [XmlElement("ICMS00")]
    public List<ICMS00> ICMS00 = new List<ICMS00>();

}

public class ICMS00
{


    public string orig { get; set; }
    public string CST { get; set; }
    public string modBC { get; set; }
    public string vBC { get; set; }
    public string pICMS { get; set; }
    public string vICMS { get; set; }

    public ICMS00() { }

    public ICMS00(string orig1, string cst, string modbc, string vbc, string picms, string vicms)
    {
        orig = orig1;
        CST = cst;
        modBC = modbc;
        vBC = vbc;
        pICMS = picms;
        vICMS = vicms;
    }

}

public class IPI
{
    // [XmlElement(Order = 1)]
    public string cEnq { get; set; }
    // [XmlElement(Order = 2)]
    [XmlElement("IPINT")]
    public List<IPINT> IPINT = new List<IPINT>();

}

public class IPINT
{

    public string CST { get; set; }

    public IPINT() { }

    public IPINT(string cst)
    {
        CST = cst;
    }

}



public class PIS
{
    [XmlElement("PISOutr")]
    public List<PISOutr> PISOutr = new List<PISOutr>();
    [XmlElement("PISAliq")]
    public List<PISAliq> PISAliq = new List<PISAliq>();

}



public class PISOutr
{

    public string CST { get; set; }
    public string vBC { get; set; }
    public string pPIS { get; set; }
    public string vPIS { get; set; }

    public PISOutr() { }

    public PISOutr(string cst, string vbc, string ppis, string vpis)
    {
        CST = cst;
        vBC = vbc;
        pPIS = ppis;
        vPIS = vpis;

    }

}


public class PISAliq
{

    public string CST { get; set; }
    public string vBC { get; set; }
    public string pPIS { get; set; }
    public string vPIS { get; set; }

    public PISAliq() { }

    public PISAliq(string cst, string vbc, string ppis, string vpis)
    {
        CST = cst;
        vBC = vBC;
        pPIS = ppis;
        vPIS = vpis;
    }

}




public class COFINS
{
    [XmlElement("COFINSOutr")]
    public List<COFINSOutr> COFINSOutr = new List<COFINSOutr>();
    [XmlElement("COFINSAliq")]
    public List<COFINSAliq> COFINSAliq = new List<COFINSAliq>();

}

public class COFINSOutr
{


    public string CST { get; set; }
    public string vBC { get; set; }
    public string pCOFINS { get; set; }
    public string vCOFINS { get; set; }

    public COFINSOutr() { }

    public COFINSOutr(string cst, string vbc, string pcofins, string vcofins)
    {
        CST = cst;
        vBC = vbc;
        pCOFINS = pcofins;
        vCOFINS = vcofins;
    }

}


public class COFINSAliq
{

    public string CST { get; set; }
    public string vBC { get; set; }
    public string pCOFINS { get; set; }
    public string vCOFINS { get; set; }

    public COFINSAliq() { }

    public COFINSAliq(string cst, string vbc, string pcofins, string vcofins)
    {

        CST = cst;
        vBC = vbc;
        pCOFINS = pcofins;
        vCOFINS = vcofins;

    }

}

CLASSE INSTANCIADA PARA SERIALIZAR

I used a loopback in the instances below

for(int item = 0; item <= 2 ;item++){  
    nfe.infNFe.det = new det();
                   nfe.infNFe.det.nItem = item.ToString();
                   nfe.infNFe.det.prod.Add(new prod(item.ToString(), "SASASASA", "sasasa", "SASASASASAS", "SSASASASA", "SASASASAS", "SASASASAS", "SASASASAS", "SASASASA", "SASASASAS", "SASASASASA", "SASASASASAS", "SASASASA", "SASASASAS", "SASASASA", "SASASASAS", "SASASASASA", "SASASASA", "SAS", "SASASAS"));
                   nfe.infNFe.det.imposto = new imposto();
                   nfe.infNFe.det.imposto.vTotTrib = "BLA BLA BLA BLA BLA";
                   nfe.infNFe.det.imposto.ICSM = new ICMS();
                   nfe.infNFe.det.imposto.ICSM.ICMS00.Add(new ICMS00("SASASASASASA", "SASASASASAS", "SASASASAS", "SASASASASA", "SASASASAS", "ASASASASA"));
                   nfe.infNFe.det.imposto.IPI = new IPI();
                   nfe.infNFe.det.imposto.IPI.cEnq = "SASASASA";
                   nfe.infNFe.det.imposto.IPI.IPINT.Add(new IPINT(""));
                   nfe.infNFe.det.imposto.PIS = new PIS();
                   nfe.infNFe.det.imposto.PIS.PISAliq.Add(new PISAliq("SASASASASA", "SASASASASA", "SASASASASA", "SASASASASA"));
                   nfe.infNFe.det.imposto.PIS.PISOutr.Add(new PISOutr("ASASASASASA", "ASASASASASA", "ASASASASASA", "SASASASASA"));
                   nfe.infNFe.det.imposto.COFINS = new COFINS();
                   nfe.infNFe.det.imposto.COFINS.COFINSAliq.Add(new COFINSAliq("SASASASA", "SASASASA", "SASASASA", "SASASASAS"));
                   nfe.infNFe.det.imposto.COFINS.COFINSOutr.Add(new COFINSOutr("SASASASAS", "ASASASASAS", "SASASASA", "SASASASA"));

}

But the result of list serialization is just the last iteration in the for loop.

Remembering that I'm serializing the NFe class completely.

If I try to serialize only the class works.

Summarizing how to make the class a list and instantiate it, being that your childs are also ready.

    
asked by anonymous 28.05.2015 / 20:10

1 answer

4

Try this way

public sealed class NFe
{
    public infNFe infNFe;
}

public sealed class infNFe
{
    private List<det> _dets;
    public det[] det { 
         get { return _dets.toArray(); } //pegamos a variável privada que é uma lista e convertemos ela para array na hora de serializar (ou quando se acessa esta propriedade - det)
         set { _dets = value.toList(); } //supondo que você receba um array de dets, convertemos ele para lista e armazenamos na variável privada.
    }
    //Se você utilizar lista provavelmente terá problemas na serialização XML, dependendo de como esta sendo feita essa serialização, então vamos usar um array primitivo.

    public infNFe(){
       _dets = new List<det>();
    }

    public Add(det det)
    {
       _dets.add(det);
    }
}

Already in your loop, do the following:

 nfe.infNFe = new infNfe();
 for(int item = 0; item <= 2 ;item++){  
    var atual = new det();
    atual.nItem = item.ToString();
    atual.nItem.prod.Add(new prod(item.ToString(), "SASASASA", "sasasa", "SASASASASAS", "SSASASASA", "SASASASAS", "SASASASAS", "SASASASAS", "SASASASA", "SASASASAS", "SASASASASA", "SASASASASAS", "SASASASA", "SASASASAS", "SASASASA", "SASASASAS", "SASASASASA", "SASASASA", "SAS", "SASASAS"));
    atual.nItem.imposto = new imposto();
    atual.nItem.imposto.vTotTrib = "BLA BLA BLA BLA BLA";
    atual.nItem.imposto.ICSM = new ICMS();
    atual.nItem.imposto.ICSM.ICMS00.Add(new ICMS00("SASASASASASA", "SASASASASAS", "SASASASAS", "SASASASASA", "SASASASAS", "ASASASASA"));
    atual.nItem.imposto.IPI = new IPI();
    atual.nItem.imposto.IPI.cEnq = "SASASASA";
    atual.nItem.imposto.IPI.IPINT.Add(new IPINT(""));
    atual.nItem.imposto.PIS = new PIS();
    atual.nItem.imposto.PIS.PISAliq.Add(new PISAliq("SASASASASA", "SASASASASA", "SASASASASA", "SASASASASA"));
    atual.nItem.imposto.PIS.PISOutr.Add(new PISOutr("ASASASASASA", "ASASASASASA", "ASASASASASA", "SASASASASA"));
    atual.nItem.imposto.COFINS = new COFINS();
    atual.nItem.imposto.COFINS.COFINSAliq.Add(new COFINSAliq("SASASASA", "SASASASA", "SASASASA", "SASASASAS"));
    atual.nItem.imposto.COFINS.COFINSOutr.Add(new COFINSOutr("SASASASAS", "ASASASASAS", "SASASASA", "SASASASA"));


    //aqui está a mágica
    nfe.infNFe.add(atual); //aqui você usa o método add da class infNfe que criamos previamente e com isso teremos a lista de "det"s populada
}
    
28.05.2015 / 20:36