2012-08-30 135 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> 

我想讀上面的SOAP消息XmlDocument使用C#:讀取使用SOAP消息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是一個未定義的命名空間..它是d – Anirudha

+1

@anirudha含義b:總數應該是d: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:'namespace id typo,並在你的示例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

首先要創建一個類來deseralize的XML值到

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

然後,你可以利用GetElementsByTagName提取SOAP請求的主體和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); 
     } 
    }