I need to create an application that gets the stock value of a corporation using web services. I currently have a reference to the web service but I do not know how to parse the XML string I get. How do I select just the stock value?
protected void Button1_Click(object sender, EventArgs e)
{
StockRefrence.StockQuote callWeb = new StockRefrence.StockQuote();
string stock = callWeb.GetQuote("CSCO");
Label1.Text = stock;
}
The web service I'm using http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2
You probably want to use XPath:
.Net XPath Examples
Related
I currently have the following method being called whenever a location is selected.
Instead of passing the city name as the parameter to search by, I would like to search locations by using latitudes and longitudes.
I will be retrieving the latitude and longitude of the selected city.
How would I need to modify this code in order to obtain this?
Thank you
private void xgrdLocation_SelectedItemChanged(object sender, SelectedItemChangedEventArgs e)
{
this.Cursor = Cursors.AppStarting;
lblFooter.Content = "Searching ...";
pushpin = new MapPushpin();
if (xgrdLocation.SelectedItem != null)
{
City selectedCity = xgrdLocation.SelectedItem as City;
//GeocodeService.Location point = new GeocodeService.Location();
pushpin.Text = selectedCity.CityName;
searchDataProvider.Search(pushpin.Text);
//lblSelectedCity.Content = selectedCity.CityName;
}
}
If you want to get the city that a latitude/longitude value is in, you can use the Reverse Geocoding. The Bing Maps REST Location API has such a feature. https://msdn.microsoft.com/en-us/library/ff701710.aspx
You can also find documentation on how to use the REST services in .NET here: https://msdn.microsoft.com/en-us/library/jj819168.aspx
Not sure, but it looks like you might be using the old legacy SOAP services in your application to do the geocoding currently. Avoid the SOAP services, they are old, slow and nearing end of life.
I try to create a simple client for WCF REST service' that I found
here.
I Added the service reference and I wrote this code:
private void button1_Click(object sender, EventArgs e)
{
WebClient proxy = new WebClient();
string serviceURL =
string.Format("http://localhost:53215/IBookService.svc/GetBooksNames");
byte[] data = proxy.DownloadData(serviceURL);
Stream stream = new MemoryStream(data);
DataContractJsonSerializer obj =
new DataContractJsonSerializer(typeof(finalProject_ClientX.ServiceReference3.Book));
finalProject_ClientX.ServiceReference3.Book book = obj.ReadObject(stream) as finalProject_ClientX.ServiceReference3.Book;
MessageBox.Show("book ID : " + book.BookName);
}
When I run the code (press the button) I get the following error:
An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
System.Runtime.Serialization.dll
Additional information: The type
'finalProject_ClientX.ServiceReference3.Book' cannot be serialized to
JSON because its IsReference setting is 'True'. The JSON format does
not support references because there is no standardized format for
representing references. To enable serialization, disable the
IsReference setting on the type or an appropriate parent class of the
type.
When I run "http://localhost:53215/IBookService.svc/GetBooksNames" in the browser I got the books:
"["MVC Music Store - Tutorial -
v3.0","Pro.ASP.NET.MVC.3.Framework","Application Architecture Guide
v2","Gang of Four Design Patterns","CS4 Pocket Reference"]"
What is the problem?
Seems like Entity framework include the property IsReference = true when it add the attribute DataContract.
So , I would recommend you to include the JSON.Net nuget package in your project.
Then modify you code to something like:
private void button1_Click(object sender, EventArgs e)
{
WebClient proxy = new WebClient();
string serviceURL =
string.Format("http://localhost:53215/IBookService.svc/GetBooksNames");
byte[] data = proxy.DownloadData(serviceURL);
var jsonString = Encoding.UTF8.GetString(data);
IList<string> bookNames = JArray.Parse(jsonString).ToObject<IList<string>>();
//Do something with the list
//foreach (string bookName in bookNames)
//{
//}
}
I am trying using a Windows Phone 8.0 Silverlight App to scrape data from PlayStation.
However, I believe the site uses Javascript and I am having trouble accessing it using htmlagilitypack.
My code so far is:
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string htmlPageLive = "";
using (var client = new HttpClient())
{
htmlPageLive = await client.GetStringAsync("https://store.sonyentertainmentnetwork.com/#!/en-us/free-games/cid=STORE-MSF77008-PSPLUSFREEGAMES?smcid=pdc:us-en:ps-plus:sub-nav-new-arrivals");
}
HtmlDocument htmlDocumentLive = new HtmlDocument();
htmlDocumentLive.LoadHtml(htmlPageLive);
foreach (var div in htmlDocumentLive.DocumentNode.SelectNodes("//ul[#class= 'pane pane0']"))
{
PSPGames newGame = new PSPGames();
newGame.Title = div.SelectSingleNode(".//h3[#class= 'cellTitle']").InnerText.Trim();
}
lstPSPGame.ItemsSource = PSPgame;
customIndeterminateProgressBar.Visibility = Visibility.Collapsed;
}
However, the app is crashing on the 'foreach' line when it tries to look up the node 'pane pane0'.
Is it possible to scrape the data? If so, what would I need to do?
Thanks in advance.
It is not possible to scrape the data with HtmlAgilityPack since it is loaded asynchronously. What you get when querying the page is its skeleton.
What you could try is watch the network calls and see if a public webservice is called. Look out for json or xml data
I am creating a WCF web api service. My problem is that some methods return a 200/OK response, but the headers and the body are empty.
In setting up my web service, I created an ADO.NET Entity Data Model. I chose ADO.NET DbContext Generator when I added a code generation item. In the Model.tt document, I changed HashSet and ICollection to List. I built my website.
It used to be that when I coded a method to return a List of an entity (like List<Customer> or List<Employee> in the Northwind database), it worked fine. Over time, I could not return a List of any of those, and could only grab one entity. Now, it's gotten to a point where I can return a List<string> or List<int>, but not a List or an instance of any entity. When I try to get a List<AnyEntity>, the response is 200/OK, but the response headers and body are empty.
I have tried using the debugger and Firefox's Web Console. Using FF's WC, I could only get an "undefined" status code. I am not sure where to go from here.
EDIT: In trying to grab all Areas from the database, I do this:
[WebGet(UriTemplate = "areas")]
public List<a1Areas> AllAreas()
{
return context.a1Areas.ToList();
}
I would appreciate any more methods for debugging this. Thanks in advance.
Found the answer, thanks to Merlyn!
In my Global.asax file, I forgot to comment out two lines that took care of proxies and disposing of my context object. The code is below:
void Application_BeginRequest(object sender, EventArgs e)
{
var context = new AssignmentEntities();
context.Configuration.ProxyCreationEnabled = false;
HttpContext.Current.Items["_context"] = context;
}
void Application_EndRequest(object sender, EventArgs e)
{
var context = HttpContext.Current.Items["_context"] as AssignmentEntities;
if (context != null)
{
context.Dispose();
}
}
I am connecting to an ODATA Service via a C# ASP.NET application, which has service operations such as:
GetItems(int? itemID, double? price)
I can consume this without issues in my browser, e.g.
http://api.mycompany.com/companycatalogue/GetItems?itemID=4
I understand how to use LINQ to Entities to consume an ODATA service, but can't find a decent explanation of how to consume service operations like the one above in C#. I have made a web reference to the service in my Visual Studio solution.
So far, I have something like this for my usual consuming of the data:
using CompanyCatalogue; //my web reference
...
protected void Page_Load(object sender, EventArgs e)
{
CompanyCatalogueEntities dataContext = new CompanyCatalogueEntities (new Uri("http://api.mycompany.com/companycatalogue/"));
var result = from i in dataContext.Items select i; //just an example
//this is where I get into problems
var operationResults = CompanyCatalogue.GetItems(6, 20.5); //I just made this up
}
Any pointers?
OK, got the answer:
using CompanyCatalogue; //my web reference
...
protected void Page_Load(object sender, EventArgs e)
{
CompanyCatalogueEntities dataContext = new CompanyCatalogueEntities();
DataServiceQuery<GetItemsResult> q = dataContext.CreateQuery<GetItemsResult>("GetItems")
.AddQueryOption("paramName", 6)
.AddQueryOption("paramName2", 20.5);
List<GetItemsResult> items = q.Execute().ToList();
}
This may be help for you.
This sample code used to consume the service operation in the WFC Data Service. This service operation(GetRowCount) returns the integer(int) type value. input para name is "code"
var q = context.CreateQuery<int>("GetRowCount").AddQueryOption("code", "'" + serviceProvider.Code + "'");
int RecordCount = context.Execute<int>(new Uri(q.RequestUri.ToString().Replace("GetRowCount()", "GetRowCount"))).FirstOrDefault();
Have you tried using HttpWebRequest?