XML反序列化结果为NULL

时间:2018-06-12 17:31:06

标签: c# xml-serialization xml-deserialization

我的反序列化器无异常地执行,但生成的对象会出现空值。源XML清楚地显示了由于某种原因未在目标对象中设置的值。我不能为我的生活弄清楚为什么?

源XML:

<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<faultcode>soapenv:Client</faultcode>
<faultstring>Could not insert new row - duplicate value in a UNIQUE INDEX column (Unique Index:).</faultstring>
<detail>
    <axlError>
        <axlcode>-239</axlcode>
        <axlmessage>Could not insert new row - duplicate value in a UNIQUE INDEX column (Unique Index:).</axlmessage>
        <request>addRoutePartition</request>
    </axlError>
</detail>

类别:

using System.Xml.Serialization;

namespace AXLClassLibrary
{
[XmlRoot(ElementName = "Fault", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Fault
{
    [XmlElement(ElementName = "faultcode")]
    public string Faultcode { get; set; }
    [XmlElement(ElementName = "faultstring")]
    public string Faultstring { get; set; }
    [XmlElement(ElementName = "detail")]
    public Detail Detail { get; set; }
    [XmlAttribute(AttributeName = "soapenv", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Soapenv { get; set; }
}

[XmlRoot(ElementName = "axlError")]
public class AxlError
{
    [XmlElement(ElementName = "axlcode")]
    public string Axlcode { get; set; }
    [XmlElement(ElementName = "axlmessage")]
    public string Axlmessage { get; set; }
    [XmlElement(ElementName = "request")]
    public string Request { get; set; }
}

[XmlRoot(ElementName = "detail")]
public class Detail
{
    [XmlElement(ElementName = "axlError")]
    public AxlError AxlError { get; set; }
}
}

解串器代码:

                var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
                XDocument xd = XDocument.Parse(resp);
                XNamespace ns1 = "http://schemas.xmlsoap.org/soap/envelope/";
                XNode faultXML = xd.Descendants(ns1 + "Fault").DescendantNodesAndSelf().First();
                XmlRootAttribute xRoot = new XmlRootAttribute();
                xRoot.ElementName = "Fault";
                xRoot.Namespace = "http://schemas.xmlsoap.org/soap/envelope/";
                xRoot.IsNullable = false;
                resp = faultXML.ToString();


                Fault currentFault;
                XmlSerializer serializer = new XmlSerializer(typeof(Fault), xRoot);
                using (TextReader readFault = new StringReader(resp))
                {
                    currentFault = (Fault)serializer.Deserialize(readFault);
                }

Resulting Object Inspection

0 个答案:

没有答案