使用 C# 解析 SOAP 响应 [英] Parsing a SOAP Response with C#

查看:122
本文介绍了使用 C# 解析 SOAP 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been trying to use the data from an API but I have not been able to read the XML Response from it.

它的形式是:

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

我需要的是产品里面的东西(目前).

And what I need is what is inside Products (for now).

我试图使用 使用 C# 来解析 SOAP 响应(以及其他人为了不淹没它)而没有结果.

I was trying to use Using C# to parse a SOAP Response (and others to not flood this) without results.

我的代码:

    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;

我几乎可以肯定我遗漏了一些基本的东西.

I am almost sure that I am missing something basic.

任何线索将不胜感激.

谢谢.

推荐答案

在您的 XML StoreProducts 不在 XML 命名空间内,只需执行 :

In your XML StoreProducts is not within an XML namespace, just do :

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

如果内部 XML 如下所示,您提供的示例代码就会成功:

The example code you gave would have been successful if the inner XML looked like this:

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

这篇关于使用 C# 解析 SOAP 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆