Really basic web service newbie question...

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

Hi,

I'm new to webservices, and am trying to write a webservice in C# which
validates UK postcodes. The webservice asmx file contains the following
code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using Validation;
namespace markrae.webservices
{
/// <summary>
/// Summary description for validation.
/// </summary>
[WebService(Namespace="http://www.markrae.co.uk/")]
public class validation : System.Web.Services.WebService
{
CValidation objValidation = new CValidation();
public validation()
{
//CODEGEN: This call is required by the ASP.NET Web Services
Designer
InitializeComponent();
}

[WebMethod]
public string UKPostcode(string pstrPostcode)
{
return objValidation.UKPostCode(pstrPostcode);
}
}
}

and the class module behind the webservice contains the following code:

using System;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml;
namespace Validation
{
/// <summary>
/// Summary description for CValidation.
/// </summary>
public class CValidation
{
public string UKPostCode(string pstrPostCode)
{
HttpWebRequest objHttpWebRequest = null;
HttpWebResponse objHttpWebResponse = null;
StreamReader objStreamReader = null;
MemoryStream objStream = null;
XmlTextWriter objXML = null;
Regex objRegEx = null;
string strURL = "";
string strHTML = "";
string strOutput = "";

objStream = new MemoryStream();
objXML = new XmlTextWriter(objStream,System.Text.Encoding.UTF8);
objXML.Formatting = Formatting.Indented;
objXML.Indentation= 4;
objXML.Namespaces = false;
objXML.WriteStartDocument();
objXML.WriteComment("This is a comment");
objXML.WriteStartElement("", "Return", "");
objXML.WriteStartElement("", "Postcode", "");
objXML.WriteString(pstrPostCode);
objXML.WriteEndElement();
objXML.WriteStartElement("", "ReturnValue", "");
objXML.WriteString("1");
objXML.WriteEndElement();
objXML.WriteEndElement();
objXML.WriteEndDocument();
objXML.Flush();
objStream.Position = 0;
objStreamReader = new StreamReader(objStream);
return objStreamReader.ReadToEnd();
}
}
}

The above doesn't cause any errors, and pretty much works as I expect, apart
from the format of the output. It displays as follows:

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.markrae.co.uk/"><?xml version="1.0"
encoding="utf-8"?> <!--This is a comment--> <Return> <Postcode>AB1
1BB</Postcode> <ReturnValue>1</ReturnValue> </Return></string>

whereas I'd expect it to display as:

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.markrae.co.uk/"></string>
<!--This is a comment-->
<Return>
<Postcode>AB1 1BB</Postcode>
<ReturnValue>1</ReturnValue>
</Return>


I'm sure I'm missing something completely obvious!

Any assistance gratefully received.

Best regards,

Mark Rae
 
Hi Mark,

Seems to me that you are going through an awful lot of effort just to format
that return, when C# does it for you.

Create a "data only" class called Return, as follows:
namespace Validation
{
public class Return
{
string PostCode;
int ReturnValue;
}
}

Then change your definition of the UKPostCode routine a bit:
namespace Validation
{
/// <summary>
/// Summary description for CValidation.
/// </summary>
public class CValidation
{
public Return UKPostCode(string pstrPostCode)
{
Return myret = new Return();
myret.PostalCode = pstrPostCode;
myret.ReturnValue = 1;
return myret;
}
}
}

Caveat: this is air code... I didn't compile it before posting. That said,
it should be close to what you want, and will hopefully give you some ideas.

Good Luck,
--- Nick




Mark Rae said:
Hi,

I'm new to webservices, and am trying to write a webservice in C# which
validates UK postcodes. The webservice asmx file contains the following
code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using Validation;
namespace markrae.webservices
{
/// <summary>
/// Summary description for validation.
/// </summary>
[WebService(Namespace="http://www.markrae.co.uk/")]
public class validation : System.Web.Services.WebService
{
CValidation objValidation = new CValidation();
public validation()
{
//CODEGEN: This call is required by the ASP.NET Web Services
Designer
InitializeComponent();
}

[WebMethod]
public string UKPostcode(string pstrPostcode)
{
return objValidation.UKPostCode(pstrPostcode);
}
}
}

and the class module behind the webservice contains the following code:

using System;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml;
namespace Validation
{
/// <summary>
/// Summary description for CValidation.
/// </summary>
public class CValidation
{
public string UKPostCode(string pstrPostCode)
{
HttpWebRequest objHttpWebRequest = null;
HttpWebResponse objHttpWebResponse = null;
StreamReader objStreamReader = null;
MemoryStream objStream = null;
XmlTextWriter objXML = null;
Regex objRegEx = null;
string strURL = "";
string strHTML = "";
string strOutput = "";

objStream = new MemoryStream();
objXML = new XmlTextWriter(objStream,System.Text.Encoding.UTF8);
objXML.Formatting = Formatting.Indented;
objXML.Indentation= 4;
objXML.Namespaces = false;
objXML.WriteStartDocument();
objXML.WriteComment("This is a comment");
objXML.WriteStartElement("", "Return", "");
objXML.WriteStartElement("", "Postcode", "");
objXML.WriteString(pstrPostCode);
objXML.WriteEndElement();
objXML.WriteStartElement("", "ReturnValue", "");
objXML.WriteString("1");
objXML.WriteEndElement();
objXML.WriteEndElement();
objXML.WriteEndDocument();
objXML.Flush();
objStream.Position = 0;
objStreamReader = new StreamReader(objStream);
return objStreamReader.ReadToEnd();
}
}
}

The above doesn't cause any errors, and pretty much works as I expect, apart
from the format of the output. It displays as follows:

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.markrae.co.uk/"><?xml version="1.0"
encoding="utf-8"?> <!--This is a comment--> <Return> <Postcode>AB1
1BB</Postcode> <ReturnValue>1</ReturnValue> </Return></string>

whereas I'd expect it to display as:

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.markrae.co.uk/"></string>
<!--This is a comment-->
<Return>
<Postcode>AB1 1BB</Postcode>
<ReturnValue>1</ReturnValue>
</Return>


I'm sure I'm missing something completely obvious!

Any assistance gratefully received.

Best regards,

Mark Rae
 
Caveat: this is air code... I didn't compile it before posting. That said,
it should be close to what you want, and will hopefully give you some
ideas.

Thanks for the reply.
 
Back
Top