[英]How do I deserialize XML in C# Envelope->Error
我需要反序列化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);
}
命名空間必須為空字符串:
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
[XmlElement(ElementName = "faultcode", Namespace = "")]
public string Faultcode { get; set; }
[XmlElement(ElementName = "faultstring", Namespace = "")]
public string Faultstring { get; set; }
[XmlElement(ElementName = "faultactor", Namespace = "")]
public string Faultactor { get; set; }
[XmlElement(ElementName = "detail", Namespace = "")]
public string Detail { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.