5

I want to serialize object to soap request. I create a Login request. I use a xml serialization. And I do not have ideas what I should to do to change this line:

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

to this:

<tas:login>

I did this :

    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; }
    }
    }

this is my output :

<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>

And I want get this :

<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>

i have'nt idea what I should to do to change this line :

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

to this :

<tas:login>
malat
  • 12,152
  • 13
  • 89
  • 158
jpok
  • 143
  • 2
  • 2
  • 11

1 Answers1

3

You probably got this working by now but just in case. I believe all you have to do it change

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

to

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

that should change

<firm>***</firm>

to

<soapenv:firm>***</soapenv:firm>
Ray Davis
  • 79
  • 1
  • 1
  • 9