1

I have explored the net and still lost in finding a simple tutorial/guide on how to create a web service which accepts XML.

I have created the following simple web service,

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]

public class Calculator : System.Web.Services.WebService
{
    public Calculator() {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public long Add(long x, long y) {
        return x + y;
    }

}

Now I need to expand this so that the web service can accept the following XML,

<?xml version="1.0" encoding="ISO-8859-1"?>
<Root>
    <WorkOrder StatusCode="0" ID="1572058">
        <JobTypeID>2339</JobTypeID>
        <JobTypeDesc>HELPLINE (2339)</JobTypeDesc>
        <AccessRestrictions>Avoid School Run</AccessRestrictions>
        <TenantName>MRS M JOHN & MR A GREY</TenantName>
        <TenantHomeTel>01234567890</TenantHomeTel>
        <TenantMobTel>1213</TenantMobTel>
        <TenantWorkTel/>
        <OrderLines>
            <OrderLine ID="4884180">
                <OrderLineVariedFrom/>
                <Status>0</Status>
                <TradeID>2315</TradeID>     
            </OrderLine>
            <OrderLine ID="4884181">
                <OrderLineVariedFrom/>
                <Status>0</Status>
                <TradeID>2380</TradeID>
            </OrderLine>
        </OrderLines>
        <Vulnerabilities>
            <Vulnerability>3645<Vulnerability/>
        </Vulnerabilities>
    </WorkOrder>
</Root>

Any help is greatly appreciated. I'm new to this and simple guide/tutorial will be really helpful. Thanks a lot

kas_miyulu
  • 335
  • 1
  • 5
  • 27
  • https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api – Dennis May 10 '18 at 07:57
  • @Dennis this is web api? where can I define the XML format? – kas_miyulu May 10 '18 at 08:02
  • Yes, this is Web API. It supports XML-formatter out-of-the-box. Client just need to send "Accept: application/xml" header, and your service must define method, which accepts model, that could be deserialized from XML above. Using `WebService`-based services nowadays is a bad idea. – Dennis May 10 '18 at 08:24
  • @Dennis do you have a example code by any chance? Still can't get my head around how I define the XML format as above – kas_miyulu May 10 '18 at 08:34
  • If your question is "how to create C# class(-es) from given XML", see this: https://stackoverflow.com/questions/4203540/generate-c-sharp-class-from-xml – Dennis May 10 '18 at 08:50

0 Answers0