My suggestion would be to look into serialization. To me, this is the easiest route to go but you could also do something like Linq to XML or use XML Writer...but that gets into a lot of code.
The firs thing to do is take your soap/xml, go to
Xml2CSharp.com | Convert your XML Examples into XmlSerializer compatable C# Classes[
^]
and generate the classes that represent your soap envelope.
Then, using the classes that get generated, you can create objects that represent portions of your envelope and assign the values as you would to any other variable in code.
var env = new Envelope();
env.Body = new Body();
env.Body.GetRequest = new GetRequest();
env.Body.GetRequest.AccessKey = new AccessKey();
env.Body.GetRequest.Parameters = new Parameters();
env.Body.GetRequest.Parameters.Param = new List<Param>();
env.Body.GetRequest.ProductID = new ProductID();
env.Body.GetRequest.Scope = new Scope();
env.Body.GetRequest.Xsi = "http://www.w3.org/2001/XMLSchema-instance";
env.Body.GetRequest.Xsd = "http://www.w3.org/2001/XMLSchema";
env.Body.GetRequest.AccessKey.Text = "MXANJVAUCAJASPH1";
env.Body.GetRequest.ProductID.Text = "FIBRE";
env.Body.GetRequest.Scope.Text = "QUALIFY";
env.Body.GetRequest.Parameters.Param.Add(new Param()
{
Id = "DirectoryID",
Text = "LOC100056097744",
Xmlns = ""
});
env.Body.GetRequest.Parameters.Param.Add(new Param()
{
Id = "CVCID",
Text = "AggCVC000000015",
Xmlns = ""
});
var xmlString = string.Empty;
XmlSerializer xmlSerializer = new XmlSerializer(env.GetType());
using(StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, env);
xmlString = textWriter.ToString();
}
Console.WriteLine(xmlString);
I'm not including the classes for the envelope as you can get those from xml2csharp website and copy/paste it into your app.
But the above generates the following XML when it is serialized
="1.0"="utf-16"
<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<GetRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AccessKey xmlns="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd">MXANJVAUCAJASPH1</AccessKey>
<ProductID xmlns="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd">FIBRE</ProductID>
<Scope xmlns="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd">QUALIFY</Scope>
<Parameters xmlns="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd">
<Param id="DirectoryID" xmlns="https://wsm.webservice.m2.com.au/schemas/StandardDataTypes.xsd">LOC100056097744</Param>
<Param id="CVCID" xmlns="https://wsm.webservice.m2.com.au/schemas/StandardDataTypes.xsd">AggCVC000000015</Param>
</Parameters>
</GetRequest>
</Body>
</Envelope>