Build a C# Stock Quote WebService Part 1/2 - Building the Ticker (cont'd)
(Page 3 of 4 )
We then loop through each line from the stream, building our return XML Document string, but first we also "clean out" some of Yahoo's "noise" (quote) characters using the StringBuilder class: StringBuilder strContent = new StringBuilder(""); sTemp+=""; for(int i=0;i<=symbols.GetLength(0)-1;i++) { sContentTemp = strm.ReadLine(); strContent.Append(sContentTemp); for (int k=0;k<=strContent.Length-1;k++) { // remove the char from StringBuilder string if contains quite symbol (\") if(strContent[k].ToString()=="\"") strContent.Remove(k,1); } // now should have clean string, create array of elements string[] contents=strContent.ToString().Split(delim2); // Clear out our StringBuilder for next iteration strContent.Remove(0,strContent.Length); sTemp+=""; sTemp+="" +contents[0]+""; sTemp+="" +contents[1]+""; sTemp+=""+contents[2]+""; sTemp+=""; sTemp+=""+contents[4]+""; sTemp+=""+contents[5]+""; sTemp+=""+contents[6]+""; sTemp+=""+contents[7]+""; sTemp+=""+contents[8]+""; sTemp+=""+contents[9]+""; sTemp+=""+contents[10]+""; sTemp+=""+contents[11]+""; sTemp+=""+contents[13]+""; sTemp+=""+contents[14]+""; sTemp+=""+contents[15]+""; sTemp+=""+contents[16]+""; sTemp+=""; result+=sTemp; sTemp=""; } //Close the stream from the server strm.Close(); result += ""; }
Finally, we catch any exceptions, and return our result stream of valid XML (or "exception", as the case may be): catch(Exception) { //If exception occurred, provide user message result="exception"; } //Return StockQuote(s) or Exception return result ; } }
Now if you simply load this page in your browser, the .NET Platform will compile and cache it into IL (Intermediate Language) and you can see the default "Webservice Discovery Channel" page, as I like to call it. At this point, you should be able to pass the input parameters on the querystring, use a SOAP method call, or even use xmlHTTP to send the parameters to the service and get back the well-formed XML response document containing your quotes. Just be sure to delimit your quote symbols with spaces, as that's what Yahoo's server expects. Your StockQuote WebService class will take care of the rest. If you append "?SDL" to your URL to the service, you'll get a complete Service Description Language document that completely describes your webservice, its method(s), parameters and types.