【问题标题】:Soap Message DeserializationSoap 消息反序列化
【发布时间】:2014-07-03 16:52:00
【问题描述】:

我有这样的肥皂消息:

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <ShipNo_Check xmlns="http://bimgewebservices.gmb.gov.tr">          
      <shipNo>13343100VB0000000014</shipNo>
      <harborNo>1234567</harborNo>
    </ShipNo_Check>
  </s:Body>
</s:Envelope>

在我的 asp.net mvc 应用程序中,我需要序列化这个 xml。我正在使用这种方法:

public object SoapTo(string soapString) {
    IFormatter formatter;
    MemoryStream memStream = null;
    Object objectFromSoap = null;
    try {
        byte[] bytes = new byte[soapString.Length];

        Encoding.ASCII.GetBytes(soapString, 0,
                     soapString.Length, bytes, 0);
        memStream = new MemoryStream(bytes);
        formatter = new SoapFormatter();
        objectFromSoap = formatter.Deserialize(memStream);
    }
    catch (Exception exception) {
        throw exception;
    }
    finally {
        if (memStream != null) memStream.Close();
    }
    return objectFromSoap;
}

我收到以下错误:

Parse Error, no assembly associated with Xml key _P1 ShipNo_Check

我怎样才能做到这一点?

【问题讨论】:

  • 您拥有的 XML 无效:打开标签 &lt;harborNo&gt;&lt;/tasitNo&gt; 关闭。
  • 我在复制粘贴时犯了错误。我已经编辑了我的问题。

标签: c# soap deserialization


【解决方案1】:

另一种方法是使用 WCF 对其进行反序列化 - 为此您需要定义一个映射到消息正文的类。下面的代码显示了一种实现方式。

public class StackOverflow_24559375
{
    const string XML = @"<?xml version=""1.0""?>
        <s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
          <s:Body>
            <ShipNo_Check xmlns=""http://bimgewebservices.gmb.gov.tr"">          
              <shipNo>13343100VB0000000014</shipNo>
              <harborNo>1234567</harborNo>
            </ShipNo_Check>
          </s:Body>
        </s:Envelope>";

    [DataContract(Name = "ShipNo_Check", Namespace = "http://bimgewebservices.gmb.gov.tr")]
    public class ShipNo_Check
    {
        [DataMember(Name = "shipNo", Order = 1)]
        public string ShipNo { get; set; }

        [DataMember(Name = "harborNo", Order = 2)]
        public string HarborNo { get; set; }
    }

    public static void Test()
    {
        using (var reader = XmlReader.Create(new StringReader(XML)))
        {
            Message m = Message.CreateMessage(reader, int.MaxValue, MessageVersion.Soap11);
            var body = m.GetBody<ShipNo_Check>();
            Console.WriteLine(body.ShipNo + " - " + body.HarborNo);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多