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

Asked

Viewed 37 times

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 tag 'code'

   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', it can be the xml namespace ?

1 answer

0

You need to create a variable with the namespace and then concatenate it together with 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.