gpt4 book ai didi

c# - SOAP 消息反序列化

转载 作者:行者123 更新时间:2023-11-30 19:09:38 28 4
gpt4 key购买 nike

我有这样的 SOAP 消息:

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

我怎样才能做到这一点?

最佳答案

一种替代方法是使用 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);
}
}
}

关于c# - SOAP 消息反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24559375/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com