1

I have a web service in .NET which takes an array as input. The Request of the service is seens as

<soap:Body>
<DisplayNames xmlns="http://tempuri.org/">
  <Names>
    <Name>
      <FirstName>string</FirstName>
      <LastName>string</LastName>
    </Name>
    <Name>
      <FirstName>string</FirstName>
      <LastName>string</LastName>
    </Name>
  </Names>
</DisplayNames>

It is possible to remove the parent tag(NAMES) and make the request like below?

<soap:Body>
<DisplayNames xmlns="http://tempuri.org/">
    <Name>
      <FirstName>string</FirstName>
      <LastName>string</LastName>
    </Name>
    <Name>
      <FirstName>string</FirstName>
      <LastName>string</LastName>
    </Name>
</DisplayNames>

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Shivaram
  • 41
  • 5

2 Answers2

1

If you mean alter the way the web service works so that the client doesn't have to send it that way, not using the existing tools. It has to do with how the XML is mapped back to the code function.

I don't know exactly what your function looks like in code-behind, but from the XML I can assume it looks like this (guessing at the return value)

[WebMethod]
private bool DisplayNames(Name[] Names)
{
   ...
}

This particular service JUST takes an array of names. The <Names> tag specified that one input parameter. The runtime recognizes this as the expected names parameter, and is able to parse it out. What you're thinking of might work if all web services ONLY took one array parameter.

I don't know if that makes sense, but say for case of illustration that this web service also takes a Boolean named "IgnoreErrors".

[WebMethod]
private bool DisplayNames(Name[] Names, bool IgnoreErrors)
{
   ...
}

The XML would then look like this:

<soap:Body>
<DisplayNames xmlns="http://tempuri.org/">
  <Names>
    <Name>
      <FirstName>string</FirstName>
      <LastName>string</LastName>
    </Name>
    <Name>
      <FirstName>string</FirstName>
      <LastName>string</LastName>
    </Name>
  </Names>
  <ShowErrors>
    <value>false</value>
  </ShowErrors>
</DisplayNames>

Now if you were to do what you're asking, remove the tag, it would look like this:

<soap:Body>
<DisplayNames xmlns="http://tempuri.org/">
  <Name>
      <FirstName>string</FirstName>
      <LastName>string</LastName>
  </Name>
  <Name>
      <FirstName>string</FirstName>
      <LastName>string</LastName>
  </Name>
  </Names>
  <ShowErrors>
    <value>false</value>
  </ShowErrors>
</soap:Body>

When you look at that, try to picture how the Function signature would look in code-behind. Instead of one array and one boolean, this now looks like two dictionaries and a boolean value.

So, all of that said, again, the <Name> tag identifies the Name parameter in the code-behind function.

David
  • 72,686
  • 18
  • 132
  • 173
1

Changed my method

from

[WebMethod] public string DisplayNames(Name[] NameCollection) {

to

[WebMethod] public string DisplayNames([XmlElement("Names")] Name[] NameCollection) {

Shivaram
  • 41
  • 5