2012-08-30 24 views
7
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Head> 
    <h:talkId s:mustknow="1" xmlns:h="urn:schemas-test:testgate:hotel:2012-06"> 
     sfasfasfasfsfsf</h:talkId> 
    </s:Head> 
    <s:Body> 
    <bookHotelResponse xmlns="urn:schemas-test:testgate:hotel:2012-06" xmlns:d="http://someURL" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <d:bookingReference>123456</d:bookingReference> 
     <d:bookingStatus>successful</d:bookingStatus> 
     <d:price xmlns:p="moreURL"> 
     <d:total>105</d:total> 
     </d:price> 
    </bookHotelResponse> 
    </s:Body> 
</s:Envelope> 

を使用して:読むソープメッセージ私はC#を使用して上記のSOAPメッセージ<code>XmlDocument</code>を読みしようとしていますC#

XmlDocument document = new XmlDocument(); 
document.LoadXml(soapmessage); //loading soap message as string 
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable); 

manager.AddNamespace("d", "http://someURL"); 

XmlNodeList xnList = document.SelectNodes("//bookHotelResponse", manager); 
int nodes = xnList.Count; 

foreach (XmlNode xn in xnList) 
{ 
    Status = xn["d:bookingStatus"].InnerText; 
} 

カウントは常にゼロになり、それがbookingstatus値を読んでいません。

+0

特にXMLメッセージのフォーマットを修正してください(現在はほとんどが欠落しています)。 –

+0

xmlが無効です... bは未定義の名前空間です。これは – Anirudha

+1

@anirudha意味、b:合計はxmlサンプルの合計でなければなりません – dthorpe

答えて

14

BookHotelResponseは、名前空間urn:schemas-test:testgate:hotel:2012-06(デフォルトの名前空間内にありますサンプルxml)ので、クエリにその名前空間を指定する必要があります。

XmlDocument document = new XmlDocument(); 
document.LoadXml(soapmessage); //loading soap message as string 
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable); 

manager.AddNamespace("d", "http://someURL"); 
manager.AddNamespace("bhr", "urn:schemas-test:testgate:hotel:2012-06"); 

XmlNodeList xnList = document.SelectNodes("//bhr:bookHotelResponse", manager); 
int nodes = xnList.Count; 

foreach (XmlNode xn in xnList) 
{ 
    Status = xn["d:bookingStatus"].InnerText; 
} 
+1

「b:」名前空間の誤植を置き換えてをに変更した後は、あなたの例のxmlで働いてくれました。 soapmessageのxmlがあなたの例のフォーマットと一致することは確かですか? – dkackman

+0

こんにちは、それは働いて、申し訳ありませんミスあります –

3

使用LINQ2XML

bookingStatusを読み取るには、この

XElement doc = XElement.Load("yourStream.xml"); 
XNamespace s = "http://schemas.xmlsoap.org/soap/envelope/";//Envelop namespace s 
XNamespace bhr="urn:schemas-test:testgate:hotel:2012-06";//bookHotelResponse namespace 
XNamespace d="http://someURL";//d namespace 

foreach (var itm in doc.Descendants(s + "Body").Descendants(bhr+"bookHotelResponse")) 
{ 
itm.Element(d+"bookingStatus").Value;//your bookingStatus value 
} 

LINQ2XMLはいえクール ....あるん:)

+0

このメソッドもありがとうございます –

1

まず、あなたは、あなたは、SOAPリクエストのボディを抽出し、オブジェクトにリクエスト文字列をdeseralizeするGetElementsByTagNameを利用することができます

public class bookHotelResponse { 
     public int bookingReference { get; set; } 
     public int bookingStatus { get; set; } 
    } 

にXML値をdeseralizeするクラスを作成します。

private static T DeserializeInnerSoapObject<T>(string soapResponse) 
    { 
     XmlDocument xmlDocument = new XmlDocument(); 
     xmlDocument.LoadXml(soapResponse); 

     var soapBody = xmlDocument.GetElementsByTagName("soap:Body")[0]; 
     string innerObject = soapBody.InnerXml; 

     XmlSerializer deserializer = new XmlSerializer(typeof(T)); 

     using (StringReader reader = new StringReader(innerObject)) 
     { 
      return (T)deserializer.Deserialize(reader); 
     } 
    } 
関連する問題