如何在 .NET Core 中将对象序列化为 soap 编码的 xml

How to serialize an object as a soap-encoded xml in .NET Core

提问人:So_oP 提问时间:7/11/2023 最后编辑:marc_sSo_oP 更新时间:7/11/2023 访问量:153

问:

我想获得肥皂编码的 XML,但不确定如何。基于这些模型,我能够构造一个简单的 XML:

public class GamblerCheckRequestXmlModel
{
     [XmlElement("Kontekst")]
     public BlackListRequestContext BlackListRequestContext { get; set; }

     [XmlElement("PersonInformation")]
     public PersonInformationRequestXmlModel PersonInformationRequestXmlModel { get;set; 
}

public class BlackListRequestContext
{
    [XmlElement("HovedOplysningerType")]
    public BlackListInformationXmlModel BlackListInformationXmlModel { get; set; }
}

public class BlackListInformationXmlModel
{
    [XmlElement("TransaktionsID")]
    public string TransaktionId { get; set; }

    [XmlElement("TransaktionsTid")]
    public DateTime TransactionDate { get; set; }
}

public class PersonInformationRequestXmlModel
{
    public string PersonCPRNumber { get; set; }
}

我正在使用这段代码

using (StringWriter textWriter = new StringWriter())
{
    xmlSerializer.Serialize(textWriter, rofusGamblerCheckRequestXmlModel);
    requestXMLDocument = textWriter.ToString();
}

我能够生成这个 XML:

<?xml version="1.0" encoding="utf-16"?>
<GamblerCheckRequestXmlModel
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Kontekst>
        <HovedOplysningerType>
            <TransaktionsID>asss</TransaktionsID>
            <TransaktionsTid>2023-07-11T12:24:57.7311239Z</TransaktionsTid>
        </HovedOplysningerType>
    </Kontekst>
    <PersonInformation>
        <PersonCPRNumber>1112221122</PersonCPRNumber>
    </PersonInformation>
</GamblerCheckRequestXmlModel>

我怎样才能在不引入相应类的情况下获得相同但像这样编码的肥皂

<?xml version="1.0"?>
<soapEnv:Envelope
    xmlns:soapEnv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapEnv:Header />
    <soapEnv:Body>
        <ser:GamblerCheckRequest
            xmlns:ser="http://services.lur.skat.dk">
            <Kontekst>
                <ns1:HovedOplysningerType
                    xmlns:ns1="http://skat.dk/begrebsmodel/xml/schemas/kontekst/2007/05/31/">
                    <TransaktionsID>888Test1/1/0001 12:00:00 AMr684166144</TransaktionsID>
                    <TransaktionsTid>1/1/0001 12:00:00 AM</TransaktionsTid>
                </ns1:HovedOplysningerType>
            </Kontekst>
            <PersonInformation>
                <PersonCPRNumber>1611902081</PersonCPRNumber>
            </PersonInformation>
        </ser:GamblerCheckRequest>
    </soapEnv:Body>
</soapEnv:Envelope>

基本上,我需要相同的 XML,只是带有信封属性(正文、标题等)

C# .net xml SOAP XML 序列化

评论

0赞 Sergey K 7/11/2023
序列化数据对象后,该对象将成为SOAP消息的正文,然后您需要手动创建带有Header和body的完整SOAP消息,无法对其进行序列化

答: 暂无答案