Vallo
Vallo

Reputation: 1967

Can't resolve namespace while parsing a XML wih XDocument

I need some help parsing this XML.

I recieve the following string and I need to obtain the value of "MensajeError".

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <WS_SSPBA_001_SResponse xmlns="http://tempuri.org/">
      <WS_SSPBA_001_SResult>
        <Estado>boolean</Estado>
        <Mensaje>string</Mensaje>
        <CodigoError>string</CodigoError>
        <MensajeError>error1</MensajeError>
      </WS_SSPBA_001_SResult>
    </WS_SSPBA_001_SResponse>
  </soap:Body>
</soap:Envelope>

I made it to the Body tag but I can't manage to parse further down the XML

var xDocument = XDocument.Parse(resultado);
        XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
        var xElements =
            xDocument.Descendants(soapenv + "Body").First()

Anything I tried to parse que tag "" failed. I only need to retrieve the tag "MensajeError"

Thanks!

Upvotes: 1

Views: 176

Answers (1)

Jonesopolis
Jonesopolis

Reputation: 25370

You could just use the LocalName:

var nodeValue = XDocument.Parse(resultado)
                         .Descendants()
                         .First(n => n.Name.LocalName == "MensajeError")
                         .Value;

//nodeValue = "error1"

Upvotes: 1

Related Questions