Read Part of the XML and some children with LINQ repeats

2

good afternoon, I have the following XML

<Detalle>
                    <Item>
                        <NroLinDet>1</NroLinDet>
                        <CodItem>
                            <TpoCod>2</TpoCod>
                            <Cod>45136</Cod>
                        </CodItem>
                        <IndFact>3</IndFact>
                        <IndAgenteResp />
                        <NomItem>PEKITAS TOALLAS HUMEDAS 70</NomItem>
                        <DscItem>CONT:437148|DTO-FIN:4%|</DscItem>
                        <Cantidad>10</Cantidad>
                        <UniMed>UNI</UniMed>
                        <PrecioUnitario>58.00</PrecioUnitario>
                        <DescuentoPct>0</DescuentoPct>
                        <DescuentoMonto>23.20</DescuentoMonto>
                        <SubDescuento>
                            <DescTipo>2</DescTipo>
                            <DescVal>4.00</DescVal>
                        </SubDescuento>
                        <RecargoPct>0</RecargoPct>
                        <RecargoMnt>0</RecargoMnt>
                        <MontoItem>556.80</MontoItem>
                    </Item>
                    <Item>
                        <NroLinDet>2</NroLinDet>
                        <CodItem>
                            <TpoCod>3</TpoCod>
                            <Cod>45678</Cod>
                            <TpoCod>2</TpoCod>
                            <Cod>45136</Cod>
                        </CodItem>
                        <IndFact>6</IndFact>
                        <IndAgenteResp />
                        <NomItem>PEKITAS TOALLAS HUMEDAS 70</NomItem>
                        <DscItem>CONT:508195|DTO-COM:100%|DTO-FIN:4%|REF/OFE:60 u|</DscItem>
                        <Cantidad>10</Cantidad>
                        <UniMed>UNI</UniMed>
                        <PrecioUnitario>58.00</PrecioUnitario>
                        <DescuentoPct>0</DescuentoPct>
                        <DescuentoMonto>580.00</DescuentoMonto>
                        <SubDescuento>
                            <DescTipo>2</DescTipo>
                            <DescVal>4.00</DescVal>
                        </SubDescuento>
                        <SubDescuento>
                            <DescTipo>2</DescTipo>
                            <DescVal>100.00</DescVal>
                        </SubDescuento>
                        <RecargoPct>0</RecargoPct>
                        <RecargoMnt>0</RecargoMnt>
                        <MontoItem>0.00</MontoItem>
                    </Item>
                </Detalle>

I need to remove some elements and the one that is inside Coditem of which there can be up to 5 appearances. I'm not able to take out that element. Below the code I'm doing with linq but I do not know how to get the cod appearances in the same object. Can you help me? a thousand thanks

var elemento = from el in eDetalle.Descendants("Item")
                       select new 
                       {
                           NroLinDet = el.Element("NroLinDet").Value,
                           NomItem = el.Element("NomItem").Value,
                           DscItem = el.Element("DscItem").Value,
                           PrecioUnitario = el.Element("PrecioUnitario").Value,
                           Cantidad = el.Element("Cantidad").Value,
                           DescuentoMonto= el.Element("DescuentoMonto").Value,
                           DescuentoPct = el.Element("DescuentoPct").Value,
                           DescVal = el.Element("DescVal").Value,
                           MontoItem = el.Element("MontoItem").Value,
                           ACA TENDRIA QUE SACAR EL VALOR DE TODAS LAS APARICIONES DE Cod DENTRO DE CodItem
                       };

Thank you very much for your help

    
asked by Marcelo Pírez 12.12.2018 в 15:15
source

1 answer

1

The xml is not valid in the CodItem node, if you are going to have repetitions, it should be

<CodItems>
    <CodItem>
        <TpoCod>3</TpoCod>
        <Cod>45678</Cod>
    </CodItem>
    <CodItem>
        <TpoCod>2</TpoCod>
        <Cod>45136</Cod>
    </CodItem>
</CodItems>

You need to reflect a collection

Then in the linq you would use

CodItems = el.Descendants("CodItem").Select(x= >new {
                                      Tipo = x.TpoCod,
                                      Cod = x.Cod
                                     }),

on the selection you apply a new one to create each item that represents the code item

    
answered by 12.12.2018 / 16:53
source