使用C#代码中的属性生成SOAP消息

时间:2017-10-06 07:47:45

标签: c# wcf soap

我需要从C#代码生成以下SOAP消息。

<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
     <wsu:Timestamp wsu:Id="TS-3DCA13685AB558766615063665309446">
        <wsu:Created>2017-09-25T19:08:50.943Z</wsu:Created>
        <wsu:Expires>2017-09-25T20:48:50.943Z</wsu:Expires>
     </wsu:Timestamp>
     <wsse:UsernameToken wsu:Id="UsernameToken-3DCA13685AB558766615063665252595">
        <wsse:Username>prdIDXXXX</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXX</wsse:Password>
        <wsu:Created>2017-09-25T19:08:45.259Z</wsu:Created>
     </wsse:UsernameToken>
  </wsse:Security>

我尝试过以下代码:

// SOAP header class
public class Security : SoapHeader
{
    public Timestamp Timestamp { get; set; }
    public UsernameToken UsernameToken { get; set; }
}

public class Timestamp 
{
    public string Created { get; set;}
    public string Expired { get; set; }
}

public class UsernameToken
{
    public string Username { get; set; }
    public string Password { get; set; }
    public string Created { get; set; }
}

但是这不会生成SOAP XML中存在的属性。例如:Timestamp字段中的“ID”属性,或Password字段中的“Type”属性。

如何在C#代码中生成属性?

1 个答案:

答案 0 :(得分:0)

使用[XmlAttribute]将属性值呈现为xml属性。

public class UsernameToken
{
    [XmlAttribute]
    public string Id{ get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string Created { get; set; }
}