Nothing value in a query to an xml tag using linq .net

0

I have the following xml:

<ns1:TRetLote_GNRE xmlns="http://www.gnre.pe.gov.br" xmlns:ns1="http://www.gnre.pe.gov.br">
  <ns1:ambiente>1</ns1:ambiente>
  <ns1:situacaoRecepcao>
    <ns1:codigo>100</ns1:codigo>
    <ns1:descricao>Lote recebido com Sucesso</ns1:descricao>
  </ns1:situacaoRecepcao>
  <ns1:recibo>
    <ns1:numero>1600831117</ns1:numero>
    <ns1:dataHoraRecibo>2016-01-14 14:18:48</ns1:dataHoraRecibo>
    <ns1:tempoEstimadoProc>2000</ns1:tempoEstimadoProc>
  </ns1:recibo>
</ns1:TRetLote_GNRE>

And I'm using the following code to get the value of the 'code' tag

   Dim xmlBase As String = retorno.OuterXml
   Dim xXml As XElement = XElement.Parse(xmlBase)
   Dim num As String = xXml.Elements("codigo").Value

But it is returning 'Nothing', can it be the namespace of xml?

    
asked by anonymous 14.01.2016 / 18:28

1 answer

0

You need to create a variable with the namespace and then concatenate it next to the search tag:

    Dim xmlBase As String = retorno.OuterXml
    Dim xXml As XDocument = XDocument.Parse(xmlBase)

    Dim nspace As XNamespace = "http://www.gnre.pe.gov.br"

    Dim num As String = xXml.Descendants(nspace + "codigo").Value
    
14.01.2016 / 18:47