简体   繁体   中英

.NET web service methods

I'm struggling with the correct method declaration to accept a whole XML request without having many parameters (I have around 50 fields and sub-structures to send over the web service).

What is the best/simplest way to process a web service request? Eg I want to create a web service method that handles an XML file like this:

<MXPOSet>
    <PO action="Replace">
      <ALLOWRECEIPT>1</ALLOWRECEIPT>
      <BILLTO />
      <BILLTOATTN />
      <BUYAHEAD>0</BUYAHEAD>
      <BUYER>KRISHNAMURTHYP</BUYER>
      <BUYERCOMPANY />
      <POLINE>
        <ASSETNUM />
        <CATALOGCODE />
        <POCOST>
          <COSTLINENUM>1</COSTLINENUM>
          <LINECOST>520.0</LINECOST>
          <QUANTITY>1.0</QUANTITY>
        </POCOST>
      </POLINE>
      <POLINE>
        <ASSETNUM />
        <CATALOGCODE />
        <POCOST>
          <COSTLINENUM>2</COSTLINENUM>
          <LINECOST>520.0</LINECOST>
          <QUANTITY>2.0</QUANTITY>
        </POCOST>
      </POLINE>
      <POTERM>
        <DESCRIPTION>An acceptance of this order</DESCRIPTION>
        <TERMID>ITTS-PU-002</TERMID>
      </POTERM>
    </PO>
  </MXPOSet>

How can I go about processing this data without having a method header like:

<WebMethod()> _
    Public Function DoImport(p1,p2,p3......)

Option 1 - Take it in as a string

Can you take it in as a string and parse it within the web method?

[WebMethod]
public bool ImportXML(string xmldoc)
{
        /* parse string to XML objects*/
}

Option 2 - bind to a class

It's more work but you can map it to a class. See MSDN .

Eg for the XML:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <SubmitClass
      xmlns="http://msdn.microsoft.com/AYS/XEService">
      <input> 
        <child1>foo</child1> 
        <child2>bar</child2> 
      </input> 
    </SubmitClass>
  </soap:Body>
</soap:Envelope>

This would work:

public class MyClass
{
    public string child1;
    public string child2;
}

[WebMethod]
public void SubmitClass(MyClass input)
{
    // Do something with complex input parameters
    return;
}

Option 3 - XmlElement

[WebMethod]
public void SubmitXml(XmlElement input)
{ 
   return;
}

More on this in the same MSDN article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM