XML Query with Linq

0

I need to read a specific part of an XML (highlighted in red), and for this I'm using LINQ.

I'm using the following code, but I do not think I wrote it in the best way.

IEnumerable<string> teste = from item in XERetornoConsulta.Descendants("Documento").Descendants("ConsultaLote").Descendants("ListaMensagem").
                                                        Descendants("Mensagem").Elements("Mensagem")
                                                    select item.Value;

<RetNfseEasyWs xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <WebService>
   <Codigo>0</Codigo>
    <Descricao>Operação realizada com sucesso</Descricao>
 </WebService>
 <Documento>
<SearchKey>000644066096908253293390001670000028</SearchKey>
<XmlDoc>PEVudmlhckxvdGVScHNSZXNwb3N0YSB4bWxucz0iaHR0cDovL3d3dy5hYnJhc2Yub3JnLmJyL25mc2UueHNkIj4NCiAgPExpc3RhTWVuc2FnZW1SZXRvcm5vPg0KICAgIDxNZW5zYWdlbVJldG9ybm8+DQogICAgICA8Q29kaWdvPkUxODI8L0NvZGlnbz4NCiAgICAgIDxNZW5zYWdlbT5FcnJvIG5hIGF1dGVudGljYcOnw6NvLjwvTWVuc2FnZW0+DQogICAgPC9NZW5zYWdlbVJldG9ybm8+DQogIDwvTGlzdGFNZW5zYWdlbVJldG9ybm8+DQo8L0VudmlhckxvdGVScHNSZXNwb3N0YT4=</XmlDoc>
<ConsultaLote>
  <SituacaoLote>3</SituacaoLote>
  <TentativasEnvio>1</TentativasEnvio>
  <TentativasConsulta>0</TentativasConsulta>
  <ListaMensagem>
    <Mensagem>
      <Codigo>E182</Codigo>
      <Mensagem>Erro na autenticação.</Mensagem>
      <Correcao>&lt;Correcao&gt;Vazia</Correcao>
    </Mensagem>
  </ListaMensagem>
</ConsultaLote>

    
asked by anonymous 12.09.2017 / 17:30

1 answer

1

The original file has been missing some items from , and this was like this:

<?xml version="1.0" encoding="utf-8" ?>
<RetNfseEasyWs xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <WebService>
    <Codigo>0</Codigo>
    <Descricao>Operação realizada com sucesso</Descricao>
  </WebService>
  <Documento>
    <SearchKey>000644066096908253293390001670000028</SearchKey>
    <XmlDoc>PEVudmlhckxvdGVScHNSZXNwb3N0YSB4bWxucz0iaHR0cDovL3d3dy5hYnJhc2Yub3JnLmJyL25mc2UueHNkIj4NCiAgPExpc3RhTWVuc2FnZW1SZXRvcm5vPg0KICAgIDxNZW5zYWdlbVJldG9ybm8+DQogICAgICA8Q29kaWdvPkUxODI8L0NvZGlnbz4NCiAgICAgIDxNZW5zYWdlbT5FcnJvIG5hIGF1dGVudGljYcOnw6NvLjwvTWVuc2FnZW0+DQogICAgPC9NZW5zYWdlbVJldG9ybm8+DQogIDwvTGlzdGFNZW5zYWdlbVJldG9ybm8+DQo8L0VudmlhckxvdGVScHNSZXNwb3N0YT4=</XmlDoc>
    <ConsultaLote>
      <SituacaoLote>3</SituacaoLote>
      <TentativasEnvio>1</TentativasEnvio>
      <TentativasConsulta>0</TentativasConsulta>
      <ListaMensagem>
        <Mensagem>
          <Codigo>E182</Codigo>
          <Mensagem>Erro na autenticação.</Mensagem>
          <Correcao>Correcao Vazia</Correcao>
        </Mensagem>
      </ListaMensagem>
    </ConsultaLote>
  </Documento>
</RetNfseEasyWs>

Code:

string c = XDocument.Parse(xml, LoadOptions.None)
    .Descendants("Mensagem")
    .Select(a => a?.Element("Mensagem")?.Value)
    .Where(a => a != null)
    .FirstOrDefault();

Example running DotNetFiddle

12.09.2017 / 18:21