C# Class 用于 XML 序列化
C# Class for XML Serialization
第一次,很久了。
我正在尝试编写一个 C# class,在序列化时将形成此 SOAP XML 字符串:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 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">
<soap:Header>
<wsse:Security>
<wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:Username>XXXXXX</wsse:Username>
<wsse:Password>XXXXXX</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soap:Body><Request xmlns="http://www.cornerstoneondemand.com/Webservices/LO/"><corpName>XXXXXX</corpName><userName>XXXXXX</userName><loId>XXXXXX</loId></Request></soap:Body></soap:Envelope>
我使用 xmltocsharp.azurewebsites.net 生成了以下 classes 但我不太明白如果需要对 classes 进行任何修改以及如何测试是否对象将序列化为正确的格式。
[XmlRoot(ElementName="UsernameToken", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public class UsernameToken {
[XmlElement(ElementName="Username", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public string Username { get; set; }
[XmlElement(ElementName="Password", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public string Password { get; set; }
[XmlAttribute(AttributeName="wsse", Namespace="http://www.w3.org/2000/xmlns/")]
public string Wsse { get; set; }
}
[XmlRoot(ElementName="Security", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public class Security {
[XmlElement(ElementName="UsernameToken", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public UsernameToken UsernameToken { get; set; }
}
[XmlRoot(ElementName="Header", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Header {
[XmlElement(ElementName="Security", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public Security Security { get; set; }
}
[XmlRoot(ElementName="Request", Namespace="http://www.cornerstoneondemand.com/Webservices/LO/")]
public class Request {
[XmlElement(ElementName="corpName", Namespace="http://www.cornerstoneondemand.com/Webservices/LO/")]
public string CorpName { get; set; }
[XmlElement(ElementName="userName", Namespace="http://www.cornerstoneondemand.com/Webservices/LO/")]
public string UserName { get; set; }
[XmlElement(ElementName="loId", Namespace="http://www.cornerstoneondemand.com/Webservices/LO/")]
public string LoId { get; set; }
[XmlAttribute(AttributeName="xmlns")]
public string Xmlns { get; set; }
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body {
[XmlElement(ElementName="Request", Namespace="http://www.cornerstoneondemand.com/Webservices/LO/")]
public Request Request { get; set; }
}
[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope {
[XmlElement(ElementName="Header", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Header Header { get; set; }
[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName="soap", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soap { get; set; }
[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName="xsd", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName="wsa", Namespace="http://www.w3.org/2000/xmlns/")]
public string Wsa { get; set; }
[XmlAttribute(AttributeName="wsse", Namespace="http://www.w3.org/2000/xmlns/")]
public string Wsse { get; set; }
[XmlAttribute(AttributeName="wsu", Namespace="http://www.w3.org/2000/xmlns/")]
public string Wsu { get; set; }
}
我尝试使用以下代码,但它不起作用:
Envelope env = new Envelope();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Envelope));
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, env);
Console.WriteLine(textWriter.ToString());
Console.ReadKey();
}
我加了两个类赞
[XmlRoot(ElementName = "Envelope")]
public class Envelope
{
[XmlElement(ElementName = "Header")]
public Header Header { get; set; }
[XmlElement(ElementName = "Body")]
public Body Body { get; set; }
}
[XmlRoot(ElementName = "Body")]
public class Body
{
[XmlElement(ElementName = "Request")]
public Request Request { get; set; }
}
您需要像下面这样初始化您的 Envelope
Envelope env = new Envelope
{
Header = new Header
{
Security = new Security
{
UsernameToken = new UsernameToken
{
Username = "abcd",
Password = "xyz"
}
}
},
Body = new Body
{
Request = new Request
{
CorpName = "qw",
UserName = "df",
LoId = "gh"
}
}
};
第一次,很久了。
我正在尝试编写一个 C# class,在序列化时将形成此 SOAP XML 字符串:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 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">
<soap:Header>
<wsse:Security>
<wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:Username>XXXXXX</wsse:Username>
<wsse:Password>XXXXXX</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soap:Body><Request xmlns="http://www.cornerstoneondemand.com/Webservices/LO/"><corpName>XXXXXX</corpName><userName>XXXXXX</userName><loId>XXXXXX</loId></Request></soap:Body></soap:Envelope>
我使用 xmltocsharp.azurewebsites.net 生成了以下 classes 但我不太明白如果需要对 classes 进行任何修改以及如何测试是否对象将序列化为正确的格式。
[XmlRoot(ElementName="UsernameToken", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public class UsernameToken {
[XmlElement(ElementName="Username", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public string Username { get; set; }
[XmlElement(ElementName="Password", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public string Password { get; set; }
[XmlAttribute(AttributeName="wsse", Namespace="http://www.w3.org/2000/xmlns/")]
public string Wsse { get; set; }
}
[XmlRoot(ElementName="Security", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public class Security {
[XmlElement(ElementName="UsernameToken", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public UsernameToken UsernameToken { get; set; }
}
[XmlRoot(ElementName="Header", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Header {
[XmlElement(ElementName="Security", Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public Security Security { get; set; }
}
[XmlRoot(ElementName="Request", Namespace="http://www.cornerstoneondemand.com/Webservices/LO/")]
public class Request {
[XmlElement(ElementName="corpName", Namespace="http://www.cornerstoneondemand.com/Webservices/LO/")]
public string CorpName { get; set; }
[XmlElement(ElementName="userName", Namespace="http://www.cornerstoneondemand.com/Webservices/LO/")]
public string UserName { get; set; }
[XmlElement(ElementName="loId", Namespace="http://www.cornerstoneondemand.com/Webservices/LO/")]
public string LoId { get; set; }
[XmlAttribute(AttributeName="xmlns")]
public string Xmlns { get; set; }
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body {
[XmlElement(ElementName="Request", Namespace="http://www.cornerstoneondemand.com/Webservices/LO/")]
public Request Request { get; set; }
}
[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope {
[XmlElement(ElementName="Header", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Header Header { get; set; }
[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName="soap", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soap { get; set; }
[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName="xsd", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName="wsa", Namespace="http://www.w3.org/2000/xmlns/")]
public string Wsa { get; set; }
[XmlAttribute(AttributeName="wsse", Namespace="http://www.w3.org/2000/xmlns/")]
public string Wsse { get; set; }
[XmlAttribute(AttributeName="wsu", Namespace="http://www.w3.org/2000/xmlns/")]
public string Wsu { get; set; }
}
我尝试使用以下代码,但它不起作用:
Envelope env = new Envelope();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Envelope));
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, env);
Console.WriteLine(textWriter.ToString());
Console.ReadKey();
}
我加了两个类赞
[XmlRoot(ElementName = "Envelope")]
public class Envelope
{
[XmlElement(ElementName = "Header")]
public Header Header { get; set; }
[XmlElement(ElementName = "Body")]
public Body Body { get; set; }
}
[XmlRoot(ElementName = "Body")]
public class Body
{
[XmlElement(ElementName = "Request")]
public Request Request { get; set; }
}
您需要像下面这样初始化您的 Envelope
Envelope env = new Envelope
{
Header = new Header
{
Security = new Security
{
UsernameToken = new UsernameToken
{
Username = "abcd",
Password = "xyz"
}
}
},
Body = new Body
{
Request = new Request
{
CorpName = "qw",
UserName = "df",
LoId = "gh"
}
}
};