2011-07-19 15 views
4

私はこのようなXML/SOAPファイルを持っている:LINQのXMLに - 単一の要素を抽出し

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
    <SendData xmlns="http://stuff.com/stuff"> 
     <SendDataResult>True</SendDataResult> 
    </SendData> 
    </soap:Body> 
</soap:Envelope> 

私はSendDataResult値を抽出したいけど難し次のコードや他の様々な方法でそうすることを持っていますが私はもう試した。要素に値があっても常にnullを返します。

XElement responseXml = XElement.Load(responseOutputFile); 
string data = responseXml.Element("SendDataResult").Value; 

SendDataResult要素を抽出するために必要なこと。

答えて

5

あなたはFirstまたはSingle続いDescendantsを使用することができます - 現在、あなたはそれがない持っている、それがその下に直接SendDataResult要素を持っているかどうかトップレベル要素を、求めています。さらに、適切な名前空間を使用していません。これは、それを修正する必要があります

XNamespace stuff = "http://stuff.com/stuff"; 
string data = responseXml.Descendants(stuff + "SendDataResult") 
         .Single() 
         .Value; 

を別の方法として、直接ナビゲート:

XNamespace stuff = "http://stuff.com/stuff"; 
XNamespace soap = "http://www.w3.org/2003/05/soap-envelope"; 
string data = responseXml.Element(soap + "Body") 
         .Element(stuff + "SendDataResult") 
         .Value; 
関連する問題