我有这样的肥皂信息:
<?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
我怎样才能做到这一点?
答案 0 :(得分:11)
另一种方法是使用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);
}
}
}