First time, long time.
I'm trying to write a C# class that when serialized, will form this SOAP XML string:
<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>
I used xmltocsharp.azurewebsites.net to generate the following classes but I don't quite understand what if any modifications to the classes need to be made and how to test whether the objects will serialize to the correct format.
[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; }
}
I tried using the following code but it's not working:
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();
}
I have added two classes like
[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; }
}
You need to initialize your Envelope like below
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"
}
}
};