none
Leitura de Xml e alteração de Tag´s RRS feed

  • Pergunta

  • Possuo vários arquivos XML em um diretório e gostaria varer todos os arquivos e alterar duas Tags (PIS e COFINS), destes arquivos para Zero, porém como as Tags possuem o mesmo nome não estou conseguindo localizar uma solução.

    <PackageWidth/>
    <ExtraCost>15.11</ExtraCost>
    <ExtraCostDefinition>BAS</ExtraCostDefinition>
    <ExtraCost>0.25</ExtraCost>
    <ExtraCostDefinition>PIS</ExtraCostDefinition>
    <ExtraCost>1.15</ExtraCost>
    <ExtraCostDefinition>COFINS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>GRIS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>ADVA</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>ICMS</ExtraCostDefinition>
    <ValueAddedTaxAmount/>
    <ValueAddedTaxPercent/>
    <ShippedDate>20180913</ShippedDate>
    <DeliveredOnThisDate/>
    </PackageLevel>


    Desde já agradeço a ajuda!!

    Obrigado

    sexta-feira, 9 de novembro de 2018 13:34

Respostas

  • Olá COd3v8,

    Tente este método:

    private void AtualizarElemXml()
    {

       XElement root;
       root = XElement.Load("O caminho do arquivo");
       var items = from item in root.Elements("ExtraCostDefinition")
                    select item;

       foreach (var item in items)
       {
           if(item.Value == "PIS")
              item.Value = "0.00";
           if (item.Value == "COFINS")
               item.Value = "0.00";
       }
       root.Save("O caminho do arquivo");
    }

    Se te foi útil vota, se respondeu a tua pergunta, marque como resposta.

    sexta-feira, 9 de novembro de 2018 15:55
  • Olá Cod3v8,

    Isso pode ser devido a hierarquia no teu arquivo, se possível poste o seu arquivo pelo menos desde o início para saber qual é a boa forma de fazer a busca.

    Atencioso. 

    sexta-feira, 9 de novembro de 2018 19:54
  • Olá Timóteo, boa tarde 

    Muito Obrigado

    <?xml version="1.0" encoding="iso-8859-1"?>
    <Transmission>
    <userID/>
    <password/>
    <sendingPartyID>TESTEBR</sendingPartyID>
    <receivingPartyID>CLIENTE</receivingPartyID>
    <representingPartyID/>
    <transmissionControlNumber/>
    <transmissionCreationDate/>
    <transmissionStructureVersion/>
    <messageCount/>
    <isTest>1</isTest>
    <Message seq="000000001">
    <sendingPartyID>TESTEBR</sendingPartyID>
    <receivingPartyID>CLIENTE</receivingPartyID>
    <representingPartyID/>
    <messageControlNumber>1</messageControlNumber>
    <messageCreationDate>20181105114200</messageCreationDate>
    <messageStructureVersion>2.0</messageStructureVersion>
    <requestMessageControlNumber/>
    <processType/>
    <processIdentifier/>
    <messageType>US_CIV</messageType>
    <InvoiceNumber>1111</InvoiceNumber>
    <InvoiceDate>20181003</InvoiceDate>
    <ShipmentMethodOfPayment>MX</ShipmentMethodOfPayment>
    <CurrencyCode>BRL</CurrencyCode>
    <accountNumber>80142-2</accountNumber>
    <ShipperName>400814</ShipperName>
    <ShipperAddress1>CLIENTE</ShipperAddress1>
    <ShipperAddress2/>
    <ShipperCity/>
    <ShipperStateOrProvinceCode/>
    <ShipperPostalCode/>
    <ShipperCountryCode/>
    <BillToName>TESTENOME</BillToName>
    <BillToCompany>CLIENTE</BillToCompany>
    <BillToAddress1/>
    <BillToAddress2/>
    <BillToCity/>
    <BillToStateOrProvinceCode/>
    <BillToPostalCode/>
    <BillToCountryCode/>
    <RemitToName>BRASIL S.A</RemitToName>
    <RemitToID/>
    <RemitToAddress1>AV. BRASIL, 89898</RemitToAddress1>
    <RemitToAddress2/>
    <RemitToCityName>JORDANIA</RemitToCityName>
    <RemitToStateOrProvinceCode>RS</RemitToStateOrProvinceCode>
    <RemitToPostalCode>12325244</RemitToPostalCode>
    <RemitToCountryCode>BR</RemitToCountryCode>
    <TermsNetDueDate>20181203</TermsNetDueDate>
    <TermsDiscountAmount>0.00</TermsDiscountAmount>
    <PackageLevel>
    <ConsigneeAddressEntityCode>CN</ConsigneeAddressEntityCode>
    <ConsigneeName>CLIENTE SERVICOS LTDA.</ConsigneeName>
    <ConsigneeAdditionalName/>
    <ConsigneeAddress1>Estrada Teste, 970</ConsigneeAddress1>
    <ConsigneeAddress2/>
    <ConsigneeCity>BARUERI</ConsigneeCity>
    <ConsigneeStateOrProvinceCode>SP</ConsigneeStateOrProvinceCode>
    <ConsigneePostalCode>1253456</ConsigneePostalCode>
    <ConsigneeCountryCode>BR</ConsigneeCountryCode>
    <StandardCarrierCode>BRTETE</StandardCarrierCode>
    <TransportationMethodType/>
    <TrackingNumber>123546SE</TrackingNumber>
    <PackageTransportationCost/>
    <NetPackageTransportationCost>6.52</NetPackageTransportationCost>
    <DimensionPackageCost/>
    <PackageWeight/>
    <PackageLength/>
    <PackageWidth/>
    <ExtraCost>5.74</ExtraCost>
    <ExtraCostDefinition>BAS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>PIS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>COFINS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>GRIS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>ADVA</ExtraCostDefinition>
    <ExtraCost>0.78</ExtraCost>
    <ExtraCostDefinition>ICMS</ExtraCostDefinition>
    <ValueAddedTaxAmount/>
    <ValueAddedTaxPercent/>
    <ShippedDate>20180910</ShippedDate>
    <DeliveredOnThisDate/>
    <ReferenceNumberType></ReferenceNumberType>
    <ReferenceNumber>1212522222222222222222222226104365</ReferenceNumber>
    </PackageLevel>
    <PackageLevel>
    <ConsigneeAddressEntityCode>CN</ConsigneeAddressEntityCode>
    <ConsigneeName>CLIENTE SERVICOS LTDA.</ConsigneeName>
    <ConsigneeAdditionalName/>
    <ConsigneeAddress1>Estrada dos Testes, 970</ConsigneeAddress1>
    <ConsigneeAddress2/>
    <ConsigneeCity>BARUERI</ConsigneeCity>
    <ConsigneeStateOrProvinceCode>SP</ConsigneeStateOrProvinceCode>
    <ConsigneePostalCode>06423080</ConsigneePostalCode>
    <ConsigneeCountryCode>BR</ConsigneeCountryCode>
    <StandardCarrierCode>BRTETE</StandardCarrierCode>
    <TransportationMethodType/>
    <TrackingNumber>1252145646SE</TrackingNumber>
    <PackageTransportationCost/>
    <NetPackageTransportationCost>6.68</NetPackageTransportationCost>
    <DimensionPackageCost/>
    <PackageWeight/>
    <PackageLength/>
    <PackageWidth/>
    <ExtraCost>5.88</ExtraCost>
    <ExtraCostDefinition>BAS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>PIS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>COFINS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>GRIS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>ADVA</ExtraCostDefinition>
    <ExtraCost>0.80</ExtraCost>
    <ExtraCostDefinition>ICMS</ExtraCostDefinition>
    <ValueAddedTaxAmount/>
    <ValueAddedTaxPercent/>
    <ShippedDate>20180910</ShippedDate>
    <DeliveredOnThisDate/>
    <ReferenceNumberType>CTe</ReferenceNumberType>
    <ReferenceNumber>35180901599101000193570010016104371016104370</ReferenceNumber>
    </PackageLevel>
    <Summary>
    <TotalMonetarySummary>152452.70</TotalMonetarySummary>
    </Summary>
    </Message>
    </Transmission>

    segunda-feira, 12 de novembro de 2018 15:19

Todas as Respostas

  • Olá COd3v8,

    Tente este método:

    private void AtualizarElemXml()
    {

       XElement root;
       root = XElement.Load("O caminho do arquivo");
       var items = from item in root.Elements("ExtraCostDefinition")
                    select item;

       foreach (var item in items)
       {
           if(item.Value == "PIS")
              item.Value = "0.00";
           if (item.Value == "COFINS")
               item.Value = "0.00";
       }
       root.Save("O caminho do arquivo");
    }

    Se te foi útil vota, se respondeu a tua pergunta, marque como resposta.

    sexta-feira, 9 de novembro de 2018 15:55
  • Timóteo, 

    Primeiramente muito obrigado pela ajuda, porém não estou conseguindo alterar ainda. Ele não retorno nada quando entra no "var items : A enumeração não gerou resultados

    private void AtualizarElemXml()
        {
            XElement root;
            root = XElement.Load(@"C:\\Files\\teste.XML");
            var items = from item in root.Elements("ExtraCostDefinition")
                        select item;

            foreach (var item in items)
            {
                if (item.Value == "PIS")
                    item.Value = "0.00";
                if (item.Value == "COFINS")
                    item.Value = "0.00";
            }
            root.Save(@"C:\\Files\\teste.XML");
        }

    sexta-feira, 9 de novembro de 2018 17:53
  • Olá Cod3v8,

    Isso pode ser devido a hierarquia no teu arquivo, se possível poste o seu arquivo pelo menos desde o início para saber qual é a boa forma de fazer a busca.

    Atencioso. 

    sexta-feira, 9 de novembro de 2018 19:54
  • Olá Timóteo, boa tarde 

    Muito Obrigado

    <?xml version="1.0" encoding="iso-8859-1"?>
    <Transmission>
    <userID/>
    <password/>
    <sendingPartyID>TESTEBR</sendingPartyID>
    <receivingPartyID>CLIENTE</receivingPartyID>
    <representingPartyID/>
    <transmissionControlNumber/>
    <transmissionCreationDate/>
    <transmissionStructureVersion/>
    <messageCount/>
    <isTest>1</isTest>
    <Message seq="000000001">
    <sendingPartyID>TESTEBR</sendingPartyID>
    <receivingPartyID>CLIENTE</receivingPartyID>
    <representingPartyID/>
    <messageControlNumber>1</messageControlNumber>
    <messageCreationDate>20181105114200</messageCreationDate>
    <messageStructureVersion>2.0</messageStructureVersion>
    <requestMessageControlNumber/>
    <processType/>
    <processIdentifier/>
    <messageType>US_CIV</messageType>
    <InvoiceNumber>1111</InvoiceNumber>
    <InvoiceDate>20181003</InvoiceDate>
    <ShipmentMethodOfPayment>MX</ShipmentMethodOfPayment>
    <CurrencyCode>BRL</CurrencyCode>
    <accountNumber>80142-2</accountNumber>
    <ShipperName>400814</ShipperName>
    <ShipperAddress1>CLIENTE</ShipperAddress1>
    <ShipperAddress2/>
    <ShipperCity/>
    <ShipperStateOrProvinceCode/>
    <ShipperPostalCode/>
    <ShipperCountryCode/>
    <BillToName>TESTENOME</BillToName>
    <BillToCompany>CLIENTE</BillToCompany>
    <BillToAddress1/>
    <BillToAddress2/>
    <BillToCity/>
    <BillToStateOrProvinceCode/>
    <BillToPostalCode/>
    <BillToCountryCode/>
    <RemitToName>BRASIL S.A</RemitToName>
    <RemitToID/>
    <RemitToAddress1>AV. BRASIL, 89898</RemitToAddress1>
    <RemitToAddress2/>
    <RemitToCityName>JORDANIA</RemitToCityName>
    <RemitToStateOrProvinceCode>RS</RemitToStateOrProvinceCode>
    <RemitToPostalCode>12325244</RemitToPostalCode>
    <RemitToCountryCode>BR</RemitToCountryCode>
    <TermsNetDueDate>20181203</TermsNetDueDate>
    <TermsDiscountAmount>0.00</TermsDiscountAmount>
    <PackageLevel>
    <ConsigneeAddressEntityCode>CN</ConsigneeAddressEntityCode>
    <ConsigneeName>CLIENTE SERVICOS LTDA.</ConsigneeName>
    <ConsigneeAdditionalName/>
    <ConsigneeAddress1>Estrada Teste, 970</ConsigneeAddress1>
    <ConsigneeAddress2/>
    <ConsigneeCity>BARUERI</ConsigneeCity>
    <ConsigneeStateOrProvinceCode>SP</ConsigneeStateOrProvinceCode>
    <ConsigneePostalCode>1253456</ConsigneePostalCode>
    <ConsigneeCountryCode>BR</ConsigneeCountryCode>
    <StandardCarrierCode>BRTETE</StandardCarrierCode>
    <TransportationMethodType/>
    <TrackingNumber>123546SE</TrackingNumber>
    <PackageTransportationCost/>
    <NetPackageTransportationCost>6.52</NetPackageTransportationCost>
    <DimensionPackageCost/>
    <PackageWeight/>
    <PackageLength/>
    <PackageWidth/>
    <ExtraCost>5.74</ExtraCost>
    <ExtraCostDefinition>BAS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>PIS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>COFINS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>GRIS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>ADVA</ExtraCostDefinition>
    <ExtraCost>0.78</ExtraCost>
    <ExtraCostDefinition>ICMS</ExtraCostDefinition>
    <ValueAddedTaxAmount/>
    <ValueAddedTaxPercent/>
    <ShippedDate>20180910</ShippedDate>
    <DeliveredOnThisDate/>
    <ReferenceNumberType></ReferenceNumberType>
    <ReferenceNumber>1212522222222222222222222226104365</ReferenceNumber>
    </PackageLevel>
    <PackageLevel>
    <ConsigneeAddressEntityCode>CN</ConsigneeAddressEntityCode>
    <ConsigneeName>CLIENTE SERVICOS LTDA.</ConsigneeName>
    <ConsigneeAdditionalName/>
    <ConsigneeAddress1>Estrada dos Testes, 970</ConsigneeAddress1>
    <ConsigneeAddress2/>
    <ConsigneeCity>BARUERI</ConsigneeCity>
    <ConsigneeStateOrProvinceCode>SP</ConsigneeStateOrProvinceCode>
    <ConsigneePostalCode>06423080</ConsigneePostalCode>
    <ConsigneeCountryCode>BR</ConsigneeCountryCode>
    <StandardCarrierCode>BRTETE</StandardCarrierCode>
    <TransportationMethodType/>
    <TrackingNumber>1252145646SE</TrackingNumber>
    <PackageTransportationCost/>
    <NetPackageTransportationCost>6.68</NetPackageTransportationCost>
    <DimensionPackageCost/>
    <PackageWeight/>
    <PackageLength/>
    <PackageWidth/>
    <ExtraCost>5.88</ExtraCost>
    <ExtraCostDefinition>BAS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>PIS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>COFINS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>GRIS</ExtraCostDefinition>
    <ExtraCost>0.00</ExtraCost>
    <ExtraCostDefinition>ADVA</ExtraCostDefinition>
    <ExtraCost>0.80</ExtraCost>
    <ExtraCostDefinition>ICMS</ExtraCostDefinition>
    <ValueAddedTaxAmount/>
    <ValueAddedTaxPercent/>
    <ShippedDate>20180910</ShippedDate>
    <DeliveredOnThisDate/>
    <ReferenceNumberType>CTe</ReferenceNumberType>
    <ReferenceNumber>35180901599101000193570010016104371016104370</ReferenceNumber>
    </PackageLevel>
    <Summary>
    <TotalMonetarySummary>152452.70</TotalMonetarySummary>
    </Summary>
    </Message>
    </Transmission>

    segunda-feira, 12 de novembro de 2018 15:19