我想将对象序列化为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>
我不知道要更改此行该怎么做:
对此:
答案 0 :(得分:0)
您现在可能已经可以正常工作了,但以防万一。我相信您要做的一切都会改变
[XmlElement(ElementName = "firm")]
public string Firm { get; set; }
到
[XmlElement(ElementName = "firm", Namespace = v)]
public string Firm { get; set; }
应该改变的
<firm>***</firm>
到
<soapenv:firm>***</soapenv:firm>