2014-10-28 74 views
1

我需要一些幫助來解析這個XML。無法解析名稱空間,同時解析XML並顯示XDocument

我收到以下字符串,我需要獲取「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> 

我做了body標籤,但我不能設法進一步解析下來XML

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

什麼我試圖解析闕標籤「」失敗。我只需要檢索標籤「MensajeError」

謝謝!

回答

1

你可以只使用的localName:

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

//nodeValue = "error1" 
+1

謝謝!不知道財產存在。這就是我需要的。 – Vallo 2014-10-28 12:58:42