【问题标题】:Parsing a SOAP Response with C#使用 C# 解析 SOAP 响应
【发布时间】:2011-07-27 01:30:42
【问题描述】:

我一直在尝试使用 API 中的数据,但无法从中读取 XML 响应。

它的形式是:

    <?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>

而我需要的是 Products 里面的东西(现在)。

我试图使用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;

我几乎可以肯定我缺少一些基本的东西。

任何线索将不胜感激。

谢谢。

【问题讨论】:

标签: c# soap response


【解决方案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>

【讨论】:

    【解决方案2】:

    您确定需要解析 XML 吗? .NET 使用 c# 代理处理 SOAP 非常有效。

    您是否查看过 svcutil.exe 来生成代理?

    【讨论】:

      【解决方案3】:

      在我的情况下,我需要它来读取 post 请求中发送的 xml

              // 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;
      

      【讨论】:

        【解决方案4】:

        为了得到 XML 响应(你可以跳过这一步)

        string strXml; 
        using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))     {
            strXml = rd.ReadToEnd(); 
        }
        

        为了从响应字符串中提取 Products 字段 (strXml)

        Regex regex = new Regex("<Products>(.*?)</Products>");
        var regMatch = regex.Match(strXml);
        string strProductValue = regMatch.Groups[1].ToString();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-07
          • 2010-10-21
          • 2016-09-18
          • 2014-03-26
          • 1970-01-01
          • 1970-01-01
          • 2020-04-22
          相关资源
          最近更新 更多