Home > OS >  How to get a value from OuterXml result using LINQ or Lambda?
How to get a value from OuterXml result using LINQ or Lambda?

Time:10-16

How to get a value from OuterXml result using LINQ or Lambda? Especifically the 'chNFe' value from XML result below:

<?xml version="1.0"?>
    <retEnviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">
        <tpAmb>2</tpAmb>
        <verAplic>SVRSnfce202210040826</verAplic>
        <cStat>104</cStat>
        <xMotivo>Lote processado</xMotivo>
        <cUF>53</cUF>
        <dhRecbto>2022-10-15T15:58:27-03:00</dhRecbto>
           <protNFe versao="4.00">
              <infProt>
                 <tpAmb>2</tpAmb>
                 <verAplic>SVRSnfce202210040826</verAplic>
                 <chNFe>53221042418033000104650010000000011414758416</chNFe>
                 <dhRecbto>2022-10-15T15:58:27-03:00</dhRecbto>
                 <digVal>Kx Tp59EFCM58pzyBeKsjutpcOU=</digVal>
                 <cStat>395</cStat>
                <xMotivo>Rejeicao: Endereco do site da UF da consulta via QR-Code diverge do previsto</xMotivo>
              </infProt>
           </protNFe>
    </retEnviNFe>

What I have been trying, from a return of a Web Service OuterXml:

            var retorno = ws.nfeAutorizacaoLoteAsync(newNode).Result;
      
            var docC = XDocument.Parse(retorno.nfeResultMsg.OuterXml);

     
            // var movieCount = docC.Elements("protNFe").Elements("infProt").Elements("chNFe").Select(e => e.Value);

            //XElement docss = XElement.Load(docC);

            var chaveRetorno = from element in docC.Elements()
                               where element.Name == "chNFe"
                               select element;

CodePudding user response:

This will get you all of the "chNFe" elements. If there is just one you can tack on FirstOrDefault() or Single(), etc...

Since your document has a namespace you can add it to the element name:

 XNamespace ns  = "http://www.portalfiscal.inf.br/nfe"; 
 var node = docC.Descendants( ns   "chNFe");
  • Related