2

I've been asked to develop a project which involves consulting other company's web services, the format they gave for this is the following

<message>
        <serviceRequest serviceCode="service name">
              .../...
        </serviceRequest>
</message>&callerCode=21346&password=12012012

Now, I've read the following link which gives a very good answer as to how to make the request How to make HTTP POST web request , but I cant seem to figure out how to make the format I've been asked to. How can I insert more than one "value" inside the child node as inline?

<serviceRequest serviceCode="service name">
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
Gonzalo
  • 117
  • 11

1 Answers1

2

For making requests, I love to use Linq to XML: http://www.dotnetcurry.com/linq/564/linq-to-xml-tutorials-examples

Around step 9 has what you want.

If you have an ok understanding of LINQ (or can search SO) - you can use this with XDocument to create an object that you need to send.

So for something like what you want it would be:

XDocument xDoc = new XDocument(
        new XElement("message",
            new XElement("serviceRequest ", new XAttribute("serviceCode", "service name"), 
                new XElement("request", "dothing1"),
                new XElement("request", "dothing2")
                ),
            new XElement("serviceRequest ", new XAttribute("serviceCode", "service name")));

Which should create something like so:

<message>
    <serviceRequest serviceCode="service name">
          <request>"dothing1"</request>
          <request>"dothing2"</request>
    </serviceRequest>
    <serviceRequest serviceCode="service name">
    </serviceRequest>
</message>
Poat
  • 327
  • 1
  • 11
  • this is exactly what i needed! i looked at that tutorial for Linq and didnt quite get the grasp of it, ill give it a try next week and mark your answer, thanks a lot. – Gonzalo May 04 '18 at 18:43
  • Do you have an example xml output you’re needing exactly? Can show you a simple working example if that’ll help you grasp it better – Poat May 04 '18 at 19:11
  • Awesome, glad to hear! – Poat May 07 '18 at 12:24