Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用HttpClient消费SOAP服务_C#_Xml_Web Services_Soap_Httpclient - Fatal编程技术网

C# 使用HttpClient消费SOAP服务

C# 使用HttpClient消费SOAP服务,c#,xml,web-services,soap,httpclient,C#,Xml,Web Services,Soap,Httpclient,我对来自SOAP响应的数组的反序列化有问题。它返回null,但作为响应,我实际上可以看到正确的xml消息。也许有人能看出我的代码有什么问题。提前谢谢 SOAP响应: <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.or

我对来自SOAP响应的数组的反序列化有问题。它返回null,但作为响应,我实际上可以看到正确的xml消息。也许有人能看出我的代码有什么问题。提前谢谢

SOAP响应:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns-wp="imcwp" xmlns:ns-hostax="imchostax" xmlns:ns-ilms="imcilms" xmlns:ns-qtms="imcqtms" xmlns:ns-tptms="imctptms">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ns-wp:strTrailerRequest-TrailerResponse>
         <ns-wp:Trailer>
            <ns-wp:TrailerId>T001</ns-wp:TrailerId>
            <ns-wp:TrailerType>Flat Extender</ns-wp:TrailerType>
         </ns-wp:Trailer>
         <ns-wp:Trailer>
            <ns-wp:TrailerId>T002</ns-wp:TrailerId>
            <ns-wp:TrailerType>Flat Extender</ns-wp:TrailerType>
         </ns-wp:Trailer>
         <ns-wp:Trailer>
            <ns-wp:TrailerId>T003</ns-wp:TrailerId>
            <ns-wp:TrailerType>Flat Extender</ns-wp:TrailerType>
         </ns-wp:Trailer>
      </ns-wp:strTrailerRequest-TrailerResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

T001
扁平扩展器
T002
扁平扩展器
T003
扁平扩展器
响应模型:

[XmlRoot(ElementName = "strTrailerRequest-TrailerResponse", Namespace = "imcwp")]
public class strTrailerRequestTrailerResponse
{
    [XmlArray("strTrailerRequest-TrailerResponse", Namespace = "imcwp")]
    [XmlArrayItem("Trailer", Namespace = "imcwp")]
    public List<Trailer> Trailers { get; set; }
}
[XmlRoot(ElementName=“strTrailerRequest TrailerResponse”,Namespace=“imcwp”)]
公共类strTrailerRequestTrailerResponse
{
[XmlArray(“strTrailerRequest TrailerResponse”,Namespace=“imcwp”)]
[XmlArrayItem(“拖车”,Namespace=“imcwp”)]
公共列表{get;set;}
}
解析方法:

    private strTrailerRequestTrailerResponse ParseTrailerResponse(string response)
    {
        var soap = XDocument.Parse(response);
        XNamespace ns = "imcwp";

        var trailerResponseNode = soap.Descendants(ns + "strTrailerRequest-TrailerResponse").FirstOrDefault().ToString();
        var result = Deserialize<strTrailerRequestTrailerResponse>(trailerResponseNode);

        return result;
    }
private strTrailerRequestTrailerResponse ParseTrailerResponse(字符串响应)
{
var soap=XDocument.Parse(响应);
xns=“imcwp”;
var trailerResponseNode=soap.substands(ns+“strTrailerRequest TrailerResponse”).FirstOrDefault().ToString();
var结果=反序列化(trailerResponseNode);
返回结果;
}

对于简单xml,可以使用xml linq。见下面的代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace Certificate
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            XDocument doc = XDocument.Parse(xml);

            XElement soap = doc.Root;
            XNamespace ns = soap.GetNamespaceOfPrefix("ns-wp");


            List<Trailer> trailers = doc.Descendants(ns + "Trailer").Select(x => new Trailer()
            {
                trailerId = (string)x.Element(ns + "TrailerId"),
                trailerType = (string)x.Element(ns + "TrailerType")
            }).ToList();
        }

    }
    public class Trailer
    {
        public string trailerId { get; set; }
        public string trailerType { get;set;}

    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Xml;
使用System.Xml.Linq;
使用System.IO;
命名空间证书
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
字符串xml=File.ReadAllText(文件名);
XDocument doc=XDocument.Parse(xml);
XElement soap=doc.Root;
XNamespace ns=soap.GetNamespaceOfPrefix(“ns-wp”);
列出拖车=文档子体(ns+“拖车”)。选择(x=>new Trail()
{
trailerId=(字符串)x.Element(ns+“trailerId”),
trailerType=(字符串)x.Element(ns+“trailerType”)
}).ToList();
}
}
公共级拖车
{
公共字符串trailerId{get;set;}
公共字符串trailerType{get;set;}
}
}

为什么不反序列化整个对象,这样就不需要xDocument和查询:

var envelop = Deserialize<Envelope>(response);
foreach (var strTrailerRequestTrailerResponseTrailer in envelop.Body.strTrailerRequestTrailerResponse)
{

}

你的反序列化方法丢失了。你检查我的答案了吗?是的,谢谢。我实际需要做的就是修改模型,使其看起来像您提供的模型。
public static T Deserialize<T>(string response)
{
    var serializer = new XmlSerializer(typeof(T));
    using(TextReader reader = new StringReader(response))
    {
        return (T)serializer.Deserialize(reader);
    }
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "imcwp")]
public partial class strTrailerRequestTrailerResponseTrailer
{
    public string TrailerId { get; set; }

    public string TrailerType { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "imcwp")]
[System.Xml.Serialization.XmlRootAttribute("strTrailerRequest-TrailerResponse", Namespace = "imcwp", IsNullable = false)]
public partial class strTrailerRequestTrailerResponse
{
    [System.Xml.Serialization.XmlElementAttribute("Trailer")]
    public strTrailerRequestTrailerResponseTrailer[] Trailer { get; set; }
}