I need to get 1000's of XML files stored in a sql server database through a .Net web service for a call from iPhone? Please suggest a better way to handle it in .net web service?
views:
69answers:
1Sounds like you're writing an iPhone application?
You won't need a SOAP web service for this. You hadn't specified whether the list of thousands of xml files is known by the client at the time of request. This answer assumes that the client explicitly knows the list.
Client
Find an existing module or code that will allow the iPhone app to read XML from an HTTP resource. You'll be hitting a URL like http://foo.com/bar/GetFile.ashx?file=1.xml
Be able to read the contents of the response as XML. Repeat this call for each xml file that you want to download to the phone.
Server
Setup a web application to handle the request for a file. Create a new .ashx web handler class to listen and handle the requests for the xml file. Here's a simple example to illustrate the server side:
public class GetFile : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse r = context.Response;
r.ContentType = "text/xml";
string requestedFile = context.Request.QueryString["file"];
//ensure you check that the value received on the querystring is valid.
string xmlFromDatabase = MyDataLayer.GetXmlByFileName(requestedFile);
//pump the XML back to the caller
r.Write(xmlFromDatabase);
}
public bool IsReusable{get{return false;}}
}
All Files Not Available At Runtime
If your client does NOT know the names of the files, simply create a new method on that handler. It could publish the list of available xml files that it's capable of serving.
http://foo.com/bar/ListFiles.ashx
public class ListFiles : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse r = context.Response;
r.ContentType = "text/plain";
//grab a listing of all the files from the database
var listFiles = MyDataLayer.GetAllXmlFilesAvailable();
foreach (var file in listFile)
{
r.Write(file + "|");
}
}
public bool IsReusable{get{return false;}}
}