【问题标题】:How do I deserialize XML in C# Envelope->Error如何在 C# Envelope->Error 中反序列化 XML
【发布时间】: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


    【解决方案1】:

    命名空间需要是一个空字符串:

        [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; }
        }
    

    【讨论】:

      猜你喜欢
      • 2015-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 2010-11-25
      相关资源
      最近更新 更多