在 .NET Core 中将 Soap 请求正文绑定到 C#

Binding Soap request body to C# in .NET Core

提问人:smith 提问时间:11/10/2023 最后编辑:marc_ssmith 更新时间:11/14/2023 访问量:36

问:

我有以下SOAP请求正文。我需要C#类来绑定数据:

enter image description here

我有以下c#类,但它不起作用:

[XmlRoot(ElementName = "Brand")]
public class Brand
{
    [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string Type { get; set; }
    [XmlText]
    public string Text { get; set; }
}

[XmlRoot(ElementName = "DatiPratica")]
[XmlType(TypeName = "ElementiDatiPratica", Namespace = "http://crogwebnew.satoprg.net/soap")]
public class DatiPratica
{
    [XmlElement(ElementName = "Brand")]
    public Brand Brand { get; set; }
    [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string Type { get; set; }
    [XmlAttribute(AttributeName = "soap", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Soap { get; set; }
}

[XmlRoot(ElementName = "RichiestaFin", Namespace = "http://www.w3.org/2001/XMLSchema")]
public class RichiestaFin
{
    [XmlElement(ElementName = "DatiPratica")]
    public DatiPratica DatiPratica { get; set; }
    [XmlAttribute(AttributeName = "encodingStyle", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public string EncodingStyle { get; set; }
}

[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
    [XmlElement(ElementName = "RichiestaFin", Namespace = "http://www.w3.org/2001/XMLSchema")]
    public RichiestaFin RichiestaFin { get; set; }
}

[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
    [XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public string Header { get; set; }
    [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public Body Body { get; set; }
    [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Xsi { get; set; }
    [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Xsd { get; set; }
    [XmlAttribute(AttributeName = "soapenv", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Soapenv { get; set; }

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

    public Envelope()
    {
        ns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
        ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        ns.Add("xsd", "http://www.w3.org/2001/XMLSchema");
    }
}
XML WCF .NET-CORE SOAP BLAZOR

评论


答:

1赞 Ale_Bianco 11/10/2023 #1

这是基于提供的 SOAP 请求结构(XmlRoot、XmlElement RichiestaFin,而不是 root)的类的更正版本。

using System.Xml.Serialization;

[XmlRoot(ElementName = "Brand", Namespace = "http://crogwebnew.satoprg.net/soap")]
public class Brand
{
    [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string Type { get; set; }

    [XmlText]
    public string Text { get; set; }
}

[XmlRoot(ElementName = "DatiPratica", Namespace = "http://crogwebnew.satoprg.net/soap")]
public class DatiPratica
{
    [XmlElement(ElementName = "Brand", Namespace = "http://crogwebnew.satoprg.net/soap")]
    public Brand Brand { get; set; }

    [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string Type { get; set; }

    [XmlAttribute(AttributeName = "soap", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Soap { get; set; }
}

[XmlRoot(ElementName = "RichiestaFin", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class RichiestaFin
{
    [XmlElement(ElementName = "DatiPratica", Namespace = "http://crogwebnew.satoprg.net/soap")]
    public DatiPratica DatiPratica { get; set; }
}

[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
    [XmlElement(ElementName = "RichiestaFin", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public RichiestaFin RichiestaFin { get; set; }
}

[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
    [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public Body Body { get; set; }

    [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Xsi { get; set; }

    [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Xsd { get; set; }

    [XmlAttribute(AttributeName = "soapenv", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Soapenv { get; set; }

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

    public Envelope()
    {
        ns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
        ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        ns.Add("xsd", "http://www.w3.org/2001/XMLSchema");
    }
}

评论

0赞 smith 11/10/2023
不幸的是,它不起作用。
0赞 QI You 11/14/2023 #2

您可以尝试 MessageHeader.CreateHeader 方法。

使用此重载方法。CreateHeader(字符串、字符串、对象、 XmlObjectSerializer)

参数

name (String):标头 XML 元素的本地名称。

ns (String):标头 XML 元素的命名空间 URI。

value (Object):要创建的标头的内容。

序列化程序 (XmlObjectSerializer):用于序列化标头的 XmlObjectSerializer。

本文档对您有帮助:

MessageHeader.CreateHeader 方法