【问题标题】:SOAP response has to bind to the modelSOAP 响应必须绑定到模型
【发布时间】:2020-04-23 06:07:42
【问题描述】:

我有这个 SOAP 响应

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <ManageTerminalResponse xmlns="http://tpress.com.tx/">
            <ManageTerminalResult xmlns:a="http://schemas.data." xmlns:i="http://www.w3.org//XMLSchema-instance">
                <a:Checksum>2f28fff499684378d7fc89742773589a</a:Checksum>
                <a:Message>Success</a:Message>
                <a:ResponseCode>0000</a:ResponseCode>
                <a:ServerDate>20200105</a:ServerDate>
                <a:ServerTime>120931</a:ServerTime>
                <a:WorkKey>QlJgkae+3+Sksgta52t/FoFL2eA/eeuvu3ek6aFgSp8dGdsBrIA==</a:WorkKey>
            </ManageTerminalResult>
        </ManageTerminalResponse>
    </s:Body>
</s:Envelope>

我已经创建了如下所示的类

{
    [XmlRoot("Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class TResponse
    {
        public ResponseBody Body { get; set; }
    }
    public class ResponseBody
    {
        [XmlElement(ElementName = "ManageTerminalResponse", Namespace = "http://t.com.tw/")]
        public ManagterminalResponse ManageTerminalResponse { get; set; }
    }
    public class ManagterminalResponse
    {
        [XmlElement(ElementName = "ManageTerminalResult",Namespace = "http://schemas.datacontract.http://www.w3.org/2001/XMLSchema-instance")]
        public ManageTerminalRslt ManageTerminalResult { get; set; }
    }

    public class ManageTerminalRslt
    {
        [XmlElement(Namespace = "")]
        public string Checksum { get; set; }

        [XmlElement(Namespace = "")]
        public string Message { get; set; }

        [XmlElement(Namespace = "")]
        public string ResponseCode { get; set; }

        [XmlElement(Namespace = "")]
        public string WorkKey { get; set; }
    }
}

我需要将响应中的值传递给ManageTerminalRslt 类。 如何在 .net 3.5 框架中执行反序列化。

我提到了这个link。但是对于XmlDeserializeFromString() 表示需要将c#版本升级到6.0

帮助我使用其他功能/代码来执行上述任务。

【问题讨论】:

  • 你不能为这个 SOAP 服务创建一个合适的代理客户端吗?如果您有 wsdl,您可以将服务引用添加到您的项目或使用 svcutil.exe 生成一个代理来为您处理。它基本上只是变成了一个简单的方法调用。

标签: c# xml wcf soap deserialization


【解决方案1】:

这是工作代码。我不得不更改 c# 中的一些命名空间才能开始工作:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {

            string responseString = File.ReadAllText(FILENAME);
            StringReader sReader = new StringReader(responseString);

            XmlReader reader = XmlReader.Create(sReader);
            XmlSerializer serializer = new XmlSerializer(typeof(TResponse));
            TResponse tResponse = (TResponse)serializer.Deserialize(reader);
        }
    }
    [XmlRoot("Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class TResponse
    {
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public ResponseBody Body { get; set; }
    }
    public class ResponseBody
    {
        [XmlElement(ElementName = "ManageTerminalResponse", Namespace = "http://tpress.com.tx/")]
        public ManagterminalResponse ManageTerminalResponse { get; set; }
    }
    public class ManagterminalResponse
    {
        [XmlElement(ElementName = "ManageTerminalResult", Namespace = "http://tpress.com.tx/")]
        public ManageTerminalRslt ManageTerminalResult { get; set; }
    }

    public class ManageTerminalRslt
    {
        [XmlElement(Namespace = "http://schemas.data.")]
        public string Checksum { get; set; }

        [XmlElement(Namespace = "http://schemas.data.")]
        public string Message { get; set; }

        [XmlElement(Namespace = "http://schemas.data.")]
        public string ResponseCode { get; set; }

        [XmlElement(Namespace = "http://schemas.data.")]
        public string WorkKey { get; set; }
    }
}

【讨论】:

    猜你喜欢
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多