c# - 将对象c#序列化为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>
我不知道我应该怎么做才能改变这一行:
对此:
解决方案
您现在可能已经完成了这项工作,但以防万一。我相信你所要做的一切都会改变
[XmlElement(ElementName = "firm")]
public string Firm { get; set; }
至
[XmlElement(ElementName = "firm", Namespace = v)]
public string Firm { get; set; }
那应该改变
<firm>***</firm>
至
<soapenv:firm>***</soapenv:firm>
推荐阅读
- python - 如何使用 PyWavelets 读取图像?
- excel - 如何在单个 Excel 单元格中解析具有相同名称和列表的 XML 节点值
- firebase - 无法将数据发布到 Firebase
- excel - 如果它们在数值范围内,如何清除VBA中的单元格内容
- gdb - 如何打印特定可共享的变量?
- aws-appsync - 我可以使用 AppSync 预先缓存数据以在移动应用程序中离线工作吗?
- c - 输出背后的逻辑是什么
- react-native - 反应原生如何在相对内部设置绝对?
- python - Python中360扇区的范围重叠
- asp.net - 布尔属性在 ASP.NET 中是否需要引号?