2011-07-27 197 views
0

我一直在尝试使用API​​中的数据,但是我一直无法从中读取XML响应。用C解析SOAP响应#

它卡梅斯形式:

<?xml version="1.0" standalone="no"?> 
     <SOAP-ENV:Envelope xmlns:SOAPSDK1="http://www.w3.org/2001/XMLSchema" xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAPSDK3="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
     <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
      <SOAPSDK4:GetStoreProductsResponse xmlns:SOAPSDK4="http://www.externalwebservice.com/message/"> 
       <StoreProducts> 
        <StoreID></StoreID> 
        <Products></Products> 
       </StoreProducts> 
      </SOAPSDK4:GetStoreProductsResponse></SOAP-ENV:Body> 
     </SOAP-ENV:Envelope> 

而我需要的是里面是什么产品(现在)。

我试图使用Using C# to parse a SOAP Response(和其他人没有洪水这个)没有结果。

我的代码:

XDocument tst = XDocument.Load("Response.xml"); 
    XNamespace xmlns = "http://schemas.xmlsoap.org/soap/envelope/"; 
    var tstr = from result in tst.Descendants(xmlns + "StoreProducts") select result.Element("Products").Value; 

我几乎可以肯定,我失去了一些东西基本。

任何线索将非常感激。

谢谢。

+0

你检查这个链接发送的XML? http://stackoverflow.com/questions/2876012/using-c-to-parse-a-soap-response – Peyman

回答

1

在你的XML StoreProducts不是XML命名空间内,只是做:

var tstr = from result in tst.Descendants("StoreProducts") 
      select result.Element("Products").Value; 

你给的示例代码将是成功的,如果内部XML是这样的:

<SOAP-ENV:StoreProducts> 
    <StoreID></StoreID> 
    <Products></Products> 
    </SOAP-ENV:StoreProducts> 
+0

谢谢。这只是我需要的。 – Elder

1

是你确定你需要解析XML?使用c#代理处理SOAP的.NET非常高效。

您是否期待svcutil.exe生成代理?

0

对我来说,我需要它来读取POST请求

 // read the raw request 
     Request.InputStream.Seek(0, SeekOrigin.Begin); 
     string xmlPayload = new StreamReader(Request.InputStream).ReadToEnd(); 
     XDocument doc = XDocument.Parse(xmlPayload); 

     XNamespace xmlns = "urn:sobject.enterprise.soap.sforce.com"; 
     item.sfId = doc.Descendants(xmlns + "Id").First().Value; 
     item.AccountId = doc.Descendants(xmlns + "AccountId").First().Value; 
     item.FirstName = doc.Descendants(xmlns + "FirstName").First().Value; 
     item.LastName = doc.Descendants(xmlns + "LastName").First().Value; 
     item.XmlPayload = xmlPayload;