I am trying to generate this Soap XML:
<soapenv:Envelope xmlns:bsvc="urn:com.workday/bsvc" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1">
<wsse:UsernameToken>
<wsse:Username>TestUser02</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Password1</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
<bsvc:Workday_Common_Header>
<bsvc:Include_Reference_Descriptors_In_Response>1</bsvc:Include_Reference_Descriptors_In_Response>
</bsvc:Workday_Common_Header>
</soapenv:Header>
<soapenv:Body>
<bsvc:Get_Customers_Request bsvc:version="v42.0">
<bsvc:Request_References>
<bsvc:Customer_Reference bsvc:Descriptor="?">
<bsvc:ID bsvc:type="Customer_ID">100001</bsvc:ID>
</bsvc:Customer_Reference>
</bsvc:Request_References>
<bsvc:Response_Group>
<bsvc:Include_Reference>0</bsvc:Include_Reference>
<bsvc:Include_Customer_Data>0</bsvc:Include_Customer_Data>
<bsvc:Include_Customer_Balance>1</bsvc:Include_Customer_Balance>
<bsvc:Include_Customer_Activity_Detail>0</bsvc:Include_Customer_Activity_Detail>
</bsvc:Response_Group>
</bsvc:Get_Customers_Request>
</soapenv:Body>
</soapenv:Envelope>
But I am getting this XML using my code Below:
<soapenv:Envelope xmlns:bsvc="urn:com.workday/bsvc" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1">
<wsse:UsernameToken Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">
<wsse:Username>TestUser02</wsse:Username>
<wsse:Password>Password1</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
<bsvc:Workday_Common_Header>
<bsvc:Include_Reference_Descriptors_In_Response>1</bsvc:Include_Reference_Descriptors_In_Response>
</bsvc:Workday_Common_Header>
</soapenv:Header>
<soapenv:Body>
<bsvc:Get_Customers_Request version="v42.0">
<bsvc:Request_References>
<bsvc:Customer_Reference Descriptor="?">
<bsvc:ID type="Customer_ID">100001</bsvc:ID>
</bsvc:Customer_Reference>
</bsvc:Request_References>
<bsvc:Response_Group>
<bsvc:Include_Reference>0</bsvc:Include_Reference>
<bsvc:Include_Customer_Data>0</bsvc:Include_Customer_Data>
<bsvc:Include_Customer_Balance>1</bsvc:Include_Customer_Balance>
<bsvc:Include_Customer_Activity_Detail>0</bsvc:Include_Customer_Activity_Detail>
</bsvc:Response_Group>
</bsvc:Get_Customers_Request>
</soapenv:Body>
</soapenv:Envelope>
This is the C# Code I have:
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace WorkDayAPI
{
[XmlRoot("Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement("Header")]
public Header Header { get; set; }
[XmlElement("Body")]
public Body Body { get; set; }
}
public class Header
{
[XmlElement("Security", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public Security Security { get; set; }
[XmlElement("Workday_Common_Header", Namespace = "urn:com.workday/bsvc")]
public WorkdayCommonHeader WorkdayCommonHeader { get; set; }
}
public class Security
{
[XmlAttribute("mustUnderstand", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public string MustUnderstand { get; set; }
[XmlElement("UsernameToken", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public UsernameToken UsernameToken { get; set; }
}
public class UsernameToken
{
[XmlElement("Username")]
public string Username { get; set; }
[XmlElement("Password", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public string Password { get; set; }
[XmlAttribute("Type")]
public string PasswordType { get; set; }
}
public class WorkdayCommonHeader
{
[XmlElement("Include_Reference_Descriptors_In_Response", Namespace = "urn:com.workday/bsvc")]
public string IncludeReferenceDescriptorsInResponse { get; set; }
}
public class Body
{
[XmlElement("Get_Customers_Request", Namespace = "urn:com.workday/bsvc")]
public GetCustomersRequest GetCustomersRequest { get; set; }
}
public class GetCustomersRequest
{
[XmlElement("Request_References", Namespace = "urn:com.workday/bsvc")]
public RequestReferences RequestReferences { get; set; }
[XmlAttribute("version", Namespace = "urn:com.workday/bsvc")]
public string Version { get; set; }
[XmlElement("Response_Group", Namespace = "urn:com.workday/bsvc")]
public ResponseGroup ResponseGroup { get; set; }
}
public class RequestReferences
{
[XmlElement("Customer_Reference", Namespace = "urn:com.workday/bsvc")]
public CustomerReference CustomerReference { get; set; }
}
public class CustomerReference
{
[XmlAttribute(AttributeName = "Descriptor", Namespace = "urn:com.workday/bsvc")]
public string Descriptor { get; set; }
[XmlElement(ElementName = "ID")]
public CustomerData CustomerData { get; set; }
}
[XmlType(Namespace = "urn:com.workday/bsvc")]
public class CustomerData
{
[XmlText]
public string ID { get; set; }
[XmlAttribute(AttributeName = "type", Namespace = "urn:com.workday/bsvc")]
public string Type { get; set; }
}
public class ResponseGroup
{
[XmlElement("Include_Reference", Namespace = "urn:com.workday/bsvc")]
public string IncludeReference { get; set; }
[XmlElement("Include_Customer_Data", Namespace = "urn:com.workday/bsvc")]
public string IncludeCustomerData { get; set; }
[XmlElement("Include_Customer_Balance", Namespace = "urn:com.workday/bsvc")]
public string IncludeCustomerBalance { get; set; }
[XmlElement("Include_Customer_Activity_Detail", Namespace = "urn:com.workday/bsvc")]
public string IncludeCustomerActivityDetail { get; set; }
}
}
This is how I am setting Namespaces and Assigning Values:
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using WorkDayAPI.Input;
namespace WorkDayAPI
{
class Program
{
static void Main(string[] args)
{
GenerateSoapXmlRequest();
}
public static void GenerateSoapXmlRequest()
{
// Create the request object and populate it with values
var soapRequest = new Envelope
{
Header = new Header
{
Security = new Security
{
MustUnderstand = "1",
UsernameToken = new UsernameToken
{
Username = "TestUser02",
Password = "Password1",
PasswordType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"
}
},
WorkdayCommonHeader = new WorkdayCommonHeader
{
IncludeReferenceDescriptorsInResponse = "1"
}
},
Body = new Body
{
GetCustomersRequest = new GetCustomersRequest
{
Version = "v42.0",
RequestReferences = new RequestReferences
{
CustomerReference = new CustomerReference
{
Descriptor = "?",
CustomerData = new CustomerData
{
Type = "Customer_ID",
ID = "100001"
}
}
},
ResponseGroup = new ResponseGroup
{
IncludeReference = "0",
IncludeCustomerData = "0",
IncludeCustomerBalance = "1",
IncludeCustomerActivityDetail = "0"
}
}
}
};
// Prepare the namespaces with appropriate prefixes
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
namespaces.Add("bsvc", "urn:com.workday/bsvc");
namespaces.Add("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
// Create an XmlSerializer instance for the Envelope class
var serializer = new XmlSerializer(typeof(Envelope));
// Create a StringWriter to hold the serialized XML
using (var stringWriter = new StringWriter())
{
// Create an XmlWriterSettings object to control XML formatting
var xmlSettings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
Indent = true,
OmitXmlDeclaration = true
};
// Create an XmlWriter with the settings and namespaces
using (var xmlWriter = XmlWriter.Create(stringWriter, xmlSettings))
{
// Serialize the object to XML, passing the namespaces
serializer.Serialize(xmlWriter, soapRequest, namespaces);
}
// Get the generated XML as a string
string soapXml = stringWriter.ToString();
// Print the serialized XML to the console
Console.WriteLine(soapXml);
// Optionally, save the XML to a file
File.WriteAllText("SoapRequest.xml", soapXml);
}
}
}
}
How to Fix that Type Attribute in the password?
How to set the Namespace of bsvc in the Body Attribute?
Here is the Comparison Difference between Required XML vs generated XML:
ComparisonPic:[![enter image description here][1]][1]
2
Answers
First problem is solved by Making password as XMLText and PasswordType as XMLAttribute inside Password Class, then it geenrated XML correctly. Still not getting Namespace of BSVC Inside Body Attributes:
Are you using Visual Studio? If so you can Generate Class From JSON or XML in Visual Studio .
To create the classes from XML do the following:
Open Visual Studio
Create new Project (File => New Project => …)
Create new class (Project => Add Class…) name: Envelope.cs
Highlight the XML, right-click, and select Copy
In VS menu, click Edit
Select Paste special
Select Paste XML As Classes
You’ll get the following:
Usage:
If you’re interested in learning how to create the classes yourself , some of my other posts show how to do it.
Additional Resources: