因此,我尝试解析SOAP消息,并且找到了许多解决方案,但就我而言,其他解决方案给了我一个问题。
这是我要反序列化的消息:
<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getOrderInfoResponse xmlns="http://webservice.integrator.hkpost.com">
<getOrderInfoReturn>
<additionalDocument>NA</additionalDocument>
<certificateQty>0</certificateQty>
<counterPaidAmount>0.00</counterPaidAmount>
<countryCode>USA</countryCode>
<currencyCode>HKD</currencyCode>
<deliveryCharge>124.20</deliveryCharge>
<errMessage>Success</errMessage>
<insurAmount>5000.00</insurAmount>
<insurPermFee>11.00</insurPermFee>
<invoiceQty>0</invoiceQty>
<itemCategory>G</itemCategory>
<itemNo>JJ059363521HK</itemNo>
<orderNo>P000000001159994</orderNo>
<orderStatus>0</orderStatus>
<products>
<products>
<contentDesc>Toy</contentDesc>
<currencyCode>HKD</currencyCode>
<productCountry>China, Mainland</productCountry>
<productQty>2</productQty>
<productTariffCode>94</productTariffCode>
<productValue>10.000</productValue>
<productWeight>0.800</productWeight>
</products>
<products>
<contentDesc>Toy</contentDesc>
<currencyCode>HKD</currencyCode>
<productCountry>China, Mainland</productCountry>
<productQty>3</productQty>
<productTariffCode>94</productTariffCode>
<productValue>15.000</productValue>
<productWeight>1.000</productWeight>
</products>
</products>
<recipientAddress>Test Receipent Address</recipientAddress>
<recipientCity>New York</recipientCity>
<recipientContactNo>21234567</recipientContactNo>
<recipientCountry>USA</recipientCountry>
<recipientEmail>test-receipent@ec-ship.test</recipientEmail>
<recipientFax>22222222</recipientFax>
<recipientName>Test Receipent</recipientName>
<recipientPostalNo>10111</recipientPostalNo>
<referenceNo>ECShipApi E-Express testing</referenceNo>
<satchelAmount>0.00</satchelAmount>
<satchelTypeCode></satchelTypeCode>
<senderAddress>Flat T, Testing Block, Testing Buliding, Testing
District</senderAddress>
<senderContactNo>22222222</senderContactNo>
<senderCountry>Hong Kong SAR</senderCountry>
<senderEmail>test-sender@ec-ship.test</senderEmail>
<senderFax>22222222</senderFax>
<senderName>Peter Chan</senderName>
<senderPostalNo></senderPostalNo>
<shipCode>AEP</shipCode>
<status>0</status>
<totWeight>1.800</totWeight>
<userId>timelhk</userId>
<webPaidAmount>0.00</webPaidAmount>
</getOrderInfoReturn>
</getOrderInfoResponse>
</soapenv:Body>
</soapenv:Envelope>
这是我的代码:
[XmlType(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[XmlRoot(ElementName = "Envelope", Namespace =
"http://schemas.xmlsoap.org/soap/envelope/")]
public class SOAPEnvelope
{
[XmlAttribute(AttributeName = "soapenv", Namespace =
"http://schemas.xmlsoap.org/soap/envelope/")]
public string soapenv { get; set; }
[XmlAttribute(AttributeName = "xsd", Namespace =
"http://www.w3.org/2001/XMLSchema")]
public string xsd { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace =
"http://www.w3.org/2001/XMLSchema-instance")]
public string xsi { get; set; }
[XmlElement(ElementName = "Body", Namespace =
"http://schemas.xmlsoap.org/soap/envelope/")]
public ResponseBody<GetOrderInfoResponse> body { get; set; }
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
public SOAPEnvelope()
{
xmlns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
}
}
[XmlRoot(ElementName = "Body", Namespace =
"http://schemas.xmlsoap.org/soap/envelope/")]
public class ResponseBody<T>
{
[XmlElement(ElementName = "getOrderInfoResponse", Namespace =
"http://webservice.integrator.hkpost.com")]
public T getOrderInfoResponse { get; set; }
}
[XmlRoot(ElementName = "getOrderInfoResponse", Namespace =
"http://webservice.integrator.hkpost.com")]
public class GetOrderInfoResponse
{
[XmlElement(ElementName = "getOrderInfoReturn", Namespace = "")]
public GetOrderInfoModel getOrderInfoReturn { get; set; }
}
var rawXML = XDocument.Parse(result);
SOAPEnvelope deserializedObject;
using (var reader =
rawXML.CreateReader(System.Xml.Linq.ReaderOptions.None))
{
var ser = new XmlSerializer(typeof(SOAPEnvelope));
deserializedObject = (SOAPEnvelope)ser.Deserialize(reader);
}
List<GetOrderInfoModel> models = new List<GetOrderInfoModel>();
models.Add(deserializedObject.body.getOrderInfoResponse.getOrderInfoReturn);
return View(models);
我已经找到了许多类似的解决方案,但是唯一的区别是在body元素之后是需要转换为C#对象的对象。这样的例子行之有效。
在我的情况下,“阅读器”始终为“无”,返回值始终为null。
希望我能为您提供足够的有用信息,如果没有,请询问是否不清楚。
答案 0 :(得分:0)
您需要更新您的课程。请参见下面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string result = File.ReadAllText(FILENAME);
StringReader sReader = new StringReader(result);
SOAPEnvelope deserializedObject;
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
using (XmlReader reader = XmlReader.Create(sReader))
{
var ser = new XmlSerializer(typeof(SOAPEnvelope));
deserializedObject = (SOAPEnvelope)ser.Deserialize(reader);
}
}
}
[XmlType(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[XmlRoot(ElementName = "Envelope", Namespace ="http://schemas.xmlsoap.org/soap/envelope/")]
public class SOAPEnvelope
{
[XmlAttribute(AttributeName = "soapenv", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public string soapenv { get; set; }
[XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2001/XMLSchema")]
public string xsd { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string xsi { get; set; }
[XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public ResponseBody<GetOrderInfoResponse> body { get; set; }
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
}
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class ResponseBody<T>
{
[XmlElement(ElementName = "getOrderInfoResponse", Namespace = "http://webservice.integrator.hkpost.com")]
public T getOrderInfoResponse { get; set; }
}
[XmlRoot(ElementName = "getOrderInfoResponse", Namespace = "http://webservice.integrator.hkpost.com")]
public class GetOrderInfoResponse
{
[XmlElement(ElementName = "getOrderInfoReturn", Namespace = "http://webservice.integrator.hkpost.com")]
public GetOrderInfoReturn getOrderInfoReturn { get; set; }
}
[XmlRoot(ElementName = "getOrderInfoResponse", Namespace = "http://webservice.integrator.hkpost.com")]
public class GetOrderInfoReturn
{
[XmlElement(ElementName = "additionalDocument", Namespace = "http://webservice.integrator.hkpost.com")]
public string additionalDocument { get; set; }
[XmlElement(ElementName = "certificateQty", Namespace = "http://webservice.integrator.hkpost.com")]
public int certificateQty { get; set; }
[XmlElement(ElementName = "deliveryCharge", Namespace = "http://webservice.integrator.hkpost.com")]
public decimal deliveryCharge { get; set; }
}
}