【发布时间】:2019-07-16 01:31:36
【问题描述】:
我需要反序列化一个 SOAP 错误信封
要反序列化的 XML:
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<faultcode>9001</faultcode>
<faultstring>Some fault string</faultstring>
<faultactor>Some fault factor</faultactor>
<detail>Some Detail</detail>
</SOAP:Body>
</SOAP:Envelope>
预期的结果是将 XML 反序列化为以下类,但值没有反序列化我得到所有值的 NULL
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
[XmlElement(ElementName = "faultcode")]
public string Faultcode { get; set; }
[XmlElement(ElementName = "faultstring")]
public string Faultstring { get; set; }
[XmlElement(ElementName = "faultactor")]
public string Faultactor { get; set; }
[XmlElement(ElementName = "detail")]
public string Detail { get; set; }
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement(ElementName = "Body")]
public Body Body { get; set; }
[XmlAttribute(AttributeName = "SOAP", Namespace = "http://www.w3.org/2000/xmlns/")]
public string SOAP { get; set; }
}
反序列化代码:
var serializer = new XmlSerializer(typeof(Envelope));
using (TextReader reader = new StringReader(xmlString))
{
Envelope envelope = (Envelope)serializer.Deserialize(reader);
}
【问题讨论】:
标签: javascript c# .net xml soap