2010-05-20 102 views
3

我試圖獲得的faultcode值,faultstring,和訂單號碼從SOAP下面使用C#來解析SOAP響應

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP:Body> 
     <faultcode>1234</faultcode> 
     <faultstring>SaveOrder:SetrsOrderMain:Cannot change OrderDate if GLPeriod is closed, new OrderDate is 3/2/2010:Ln:1053</faultstring>    
     <detail> 
      <SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"> 
      <SOAP:Body UserGUID="test"> 
      <m:SaveOrder xmlns:m="http://www.test.com/software/schema/" UserGUID="test"> 
       <Order OrderNumber="1234-1234-123" Caller="" OrderStatus="A" xmlns="http://www.test.com/software/schema/"> 

這是我在C#代碼

XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath("XMLexample.xml")); 

var errorDetail = new EcourierErrorDetail 
{ 
    FaultCode = from fc in doc.Descendants("faultcode") 
       select fc.Value, 
    FaultString = from fs in c.Descendants("faultstring") 
       select fs.Value, 
    OrderNumber = from o in  
       doc.Descendants("detail").Elements("Order").Attributes("OrderNumber") 
       select o.Value 
}; 
return errorDetail; 

我能夠獲取faultcode和faultstring的值,而不是OrderNumber。我得到「枚舉沒有結果。」誰能幫忙?謝謝。

回答

6

是的,你選擇的時候忽略了XML命名空間:

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"> 
    ..... 
    <m:SaveOrder xmlns:m="http://www.test.com/software/schema/" UserGUID="test"> 
    <Order OrderNumber="1234-1234-123" Caller="" OrderStatus="A" xmlns="http://www.test.com/software/schema/"> 

<Order>標籤是使用由m:前綴爲前綴的XML命名空間<m:SaveOrder>標籤內。另外,您試圖選擇「詳細信息」,然後直接跳到「訂購」節點(使用.Elements()) - 您錯過了其間的<m:SaveOrder>節點。

選擇時,您需要考慮到這一點:

XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath("XMLexample.xml")); 

XNamespace xmlns = "http://www.test.com/software/schema/"; 

var orderNode = doc.Descendants(xmlns + "SaveOrder").Elements(xmlns + "Order"); 

var value = from o in orderNode.Attributes("OrderNumber") 
      select o.Value; 

這是否給你一個結果?

+0

作品完美!謝謝。 – Gavin 2010-05-20 17:27:24