javascript - 如何在 C# Envelope->Error 中反序列化 XML

标签 javascript c# .net xml soap

我需要反序列化 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; }
    }

关于javascript - 如何在 C# Envelope->Error 中反序列化 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57048979/

相关文章:

c# - 应用在添加 Oauth 时重定向到 Account/AccessDenied

javascript - 如何在没有时移的情况下解析数据库中的日期字符串?

javascript - 从数组填充嵌套对象?

Javascript Boolean.prototype.toString() 意外结果

c# - 如何在保持打开状态的同时将 "this.ShowInTaskBar"更改为 "form.ShowDialog()"?

c# - GUI 和 Windows 服务通信

javascript - 使用 jQuery 实现幻灯片和选项卡的最佳方法

c# - 如何确定一个 JSON 对象是否只包含一个特定的键?

.net - 如何避免进程终止通知和标准输出重定向事件之间的竞争条件?

c# - 我如何优化这个像素不透明度计算?