Click here to Skip to main content
15,886,734 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This one copied from soapui. But when i generate through code how can add the soap envelop wrapper,header and body tags?

What I have tried:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:whol="https://wsm.webservice.m2.com.au/schemas/WholesaleServiceManagement.xsd" 
xmlns:stan="https://wsm.webservice.m2.com.au/schemas/StandardDataTypes.xsd">
<soapenv:Header/>
<soapenv: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>
</soapenv:Body>
</soapenv:Envelope>
Posted
Updated 28-Jun-18 6:13am

1 solution

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

XML
<?xml version="1.0" encoding="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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900