c# - 将对象 c# 序列化为 soap 请求

标签 c# soap xml-serialization

我想将对象序列化为 soap 请求。 我创建一个登录请求。 我使用 xml 序列化。 而且我不知道我应该做什么来改变这条线:

<login xmlns="http://tasks.ws.com/">

为此:

<tas:login>

我这样做了:

    using System;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

public class Program
{
    public static void Main()
    {
        var env = new LoginRequest.Envelope
        {
            Header = new LoginRequest.Header(),
            Body = new LoginRequest.Body()
            {
                LoginRequest = new LoginRequest
                {
                 //   Id = "o0",
                 //   Root = 1,
                    DeviceId = "***",
                    Firm = "***",
                    Login = "***",
                    Password = "***"
                },
            },
        };
        var serializer = new XmlSerializer(typeof(LoginRequest.Envelope));
        var settings = new XmlWriterSettings
        {
            Encoding = Encoding.UTF8,
            Indent = true,
            OmitXmlDeclaration = true,
        };
        var builder = new StringBuilder();
        using (var writer = XmlWriter.Create(builder, settings))
        {
            serializer.Serialize(writer, env, env.xmlns);
        }
        Console.WriteLine(builder.ToString());
    }
}


[XmlType(Namespace = LoginRequest.t, IncludeInSchema = true)]
public class LoginRequest
{
    private const string i = "http://www.w3.org/2001/XMLSchema-instance";
    private const string d = "http://www.w3.org/2001/XMLSchema";
    private const string c = "http://schemas.xmlsoap.org/soap/encoding/";
    private const string v = "http://schemas.xmlsoap.org/soap/envelope/";
    private const string t = "http://tasks.ws.com/";

  //  [XmlAttribute(AttributeName = "id")]
   // public string Id { get; set; }
   // [XmlAttribute(AttributeName = "root", Namespace = c)]
   // public int Root { get; set; }

    [XmlElement(ElementName = "firm")]
    public string Firm { get; set; }

    [XmlElement(ElementName = "login")]
    public string Login { get; set; }

    [XmlElement(ElementName = "password")]
    public string Password { get; set; }

    [XmlElement(ElementName = "device_id")]
    public string DeviceId { get; set; }

    [XmlRoot(Namespace = v)]
    public class Envelope
    {
        public Header Header { get; set; }
        public Body Body { get; set; }

        static Envelope()
        {
            staticxmlns = new XmlSerializerNamespaces();
            staticxmlns.Add("i", i);
            staticxmlns.Add("d", d);
            staticxmlns.Add("c", c);
            staticxmlns.Add("soapenv", v);
        }
        private static XmlSerializerNamespaces staticxmlns;
        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces xmlns { get { return staticxmlns; } set { } }
    }

    [XmlType(Namespace = v)]
    public class Header { }

    [XmlType(Namespace = v)]
    public class Body
    {
        [XmlElement(ElementName = "login", Namespace = t)]
        public LoginRequest LoginRequest { get; set; }
    }
    }

这是我的输出:

<soapenv:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header />
  <soapenv:Body>
    <login xmlns="http://tasks.ws.com/">
      <firm>***</firm>
      <login>***</login>
      <password>***</password>
      <device_id>***</device_id>
    </login>
  </soapenv:Body>
</soapenv:Envelope>

我想得到这个:

<Envelope xmlns:soapenv="http://tasks.ws.com/" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
  <Header />
  <Body>
    <tas:login>
      <soapenv:firm>***</soapenv:firm>
      <soapenv:login>***</soapenv:login>
      <soapenv:password>***</soapenv:password>
      <soapenv:device_id>***</soapenv:device_id>
    </tas:login>
  </Body>
</Envelope>

我不知道我应该做什么来改变这条线:

<login xmlns="http://tasks.ws.com/">

为此:

<tas:login>

最佳答案

你现在可能已经开始工作了,但以防万一。我相信你要做的一切都会改变

[XmlElement(ElementName = "firm")]
public string Firm { get; set; }

[XmlElement(ElementName = "firm", Namespace = v)]
public string Firm { get; set; }

应该改变

<firm>***</firm>

<soapenv:firm>***</soapenv:firm>

关于c# - 将对象 c# 序列化为 soap 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53533520/

相关文章:

C#:Microsoft.Office.Interop.Excel.Range 的 ID 字段未与 Excel 工作表一起保存

C# 在 Windows 8 的路径中执行带空格的外部进程

c# - 通过 .NET 确定现有的本地 Windows 帐户

java - 错误 error.GrailsExceptionResolver - 无法引用隐式元素

c# - 将 AcceptChanges 与 SQL Server 结合使用

javascript - Angular ngx-soap GET 请求

java - ssl handShake 失败 SOAP

soap - 需要 kSOAP 编码帮助

c# - Color 结构的最优雅的 XML 序列化

c++ - 如果从文件流读取数据,为什么 gSOAP 将标准输入模式设置为二进制?