C# Restful Service Question. How do you get an integer value?

JC0133

Senior member
Nov 2, 2010
201
1
76
This is my first semester taking C# and working on web services. I started the semester off learning SOAP, now I am learning REST. I hear REST is the more popular one to use. So far it seems harder to user to me, but I am still very new to it.

So I am trying to create restful service. Actually creating the service is working. I want to get a return int value from the service and on my client/console side store that int value into a int arrayList.

Problem is the rest service is returning the entire xml string. like this

<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">0</int>

I just want the 0, so I can store that value in my arrayList. I feel like C# should have some simple library or something that allows me to just get the values in between the xml tags.

I wrote up test code to get me a better idea of how to get data from my REST services.


Here is code in the service contract of WCF(we are supposed to use WCF in this assignment, so I am using it in my test code).

[ServiceContract]
public interface IService1
{

[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare)]
int getTrafficAccidentsByZip(int zCode);
[OperationContract]
[WebGet(UriTemplate = "latlonValues?lonVal={lonVal}&latVal={latVal}", BodyStyle =WebMessageBodyStyle.Bare)]
int getTrafficAccidentByLonLat(double lonVal, double latVal);



// TODO: Add your service operations here
}



Here is the implementation of the contracts. Like I said, this is test code. So I am just returning 0

public class Service1 : IService1
{
public int getTrafficAccidentsByZip(int zCode)
{
return 0;
}

public int getTrafficAccidentByLonLat(double lonVal, double latValue)
{
return 0;
}

This is what I have in main. You can see below that I tried to convert the string to int. That caused an error.



string url = @"http://localhost:14722/Service1.svc/getTrafficAccidentsByZip";


HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();

StreamReader reader = new StreamReader(responseStream);
// int test = Convert.ToInt32( reader);
String json = reader.ReadToEnd();
// int test = Convert.ToInt32(json);
Console.WriteLine(json);


I feel like this should be really easy. BUT I have been having a hard time finding simple examples of how to do this online.

Any good online C# REST examples or youtube videos would be greatly appreciated. WIth examples. I learn from examples.
 

sao123

Lifer
May 27, 2002
12,648
201
106
C# has built in XML Classes... but you have to use them.
What you need to do is use the XMLDocument / XMLReader Classes to store the XML you get from the REST Service, then you can either use the class methods with XMLElement / XMLAttribute to parse the file OR use LINQ Queries to get the values you want.
Either method will then be able to store them into whatever structure you need.

See the system.xml namespace documentation for details.
https://msdn.microsoft.com/en-us/library/system.xml(v=vs.110).aspx
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Also, SOAP sounds attractive at first until you try to use it in a large project, then the nightmares begin. Especially when you are working with another company that controls the other side of the connection and is using a different SOAP toolkit than yours. That was no fun at all, either time that we did it.

There are many good reasons why almost everyone has abandoned SOAP in favor of REST. Here's one set of them:

https://www.quora.com/Is-REST-a-bet...ey-are-different-tools-for-different-purposes
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
I hear REST is the more popular one to use. So far it seems harder to user to me, but I am still very new to it.

That's because no sane person uses WCF to implement a REST service. WebAPI (or MVC Core) is the modern tool to use, but that doesn't really help you I supposed.

Problem is the rest service is returning the entire xml string. like this

The whole point of XML is that it allows you to include markup to describe your data, to ensure that it's correct and help recipients read it. In this case it's helpfully including the <int></int> tags, because if you got a value from your service that was a plain string how on earth would you possibly know that it's an integer without that extra markup (/s). As you're finding out, this is the downside to XML. In theory it sounds good, but it adds a lot of overhead and complexity, which isn't so bad when the tooling hides it, e.g. when all the crap involved with a SOAP web service just works (hopefully), but becomes a huge pain as soon as you step outside the bounds of those tools.

I just want the 0, so I can store that value in my arrayList. I feel like C# should have some simple library or something that allows me to just get the values in between the xml tags.

XMLReader
 
  • Like
Reactions: sao123

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Depending on the tools used, the XML might or might not include types. Services that return XML often just return tags with variable names like this:

<accidents>12</accidents>
<injuries>
<fatalities>1</fatalities>
</injuries>
<spills>
<toxic>0</toxic>
<beer>1</beer>
</spills>

Since both the service and the caller usually already know the types with REST.

And since this is not SOAP, the caller is often supposed to just ignore extra tags that they do not know or care about, instead of treating them as an error.
 

nakedfrog

No Lifer
Apr 3, 2001
58,215
12,394
136
Something along the lines of setting a variable of type XMLElement to the contents of that string, and then referencing the .innerXml? Can't be bothered with getting the propers, but that should be a step in the right direction.

LINQ could be good too, but that's a whole 'nother rabbit hole to dive down if you're just learning to set-up a service.
 

Apathetic

Platinum Member
Dec 23, 2002
2,587
6
81
Also, SOAP sounds attractive at first until you try to use it in a large project, then the nightmares begin. Especially when you are working with another company that controls the other side of the connection and is using a different SOAP toolkit than yours. That was no fun at all, either time that we did it.

There are many good reasons why almost everyone has abandoned SOAP in favor of REST. Here's one set of them:

https://www.quora.com/Is-REST-a-bet...ey-are-different-tools-for-different-purposes

And, of course, they're using Java which has slightly different interpretation of SOAP and how certain things are represented... Ugh. I'm with you. I don't miss this at all.

Dave
 

Apathetic

Platinum Member
Dec 23, 2002
2,587
6
81
I had a similar problem a few years back, but we were using C#, and they were using Java.... *whimpers*

Dave

Also, SOAP sounds attractive at first until you try to use it in a large project, then the nightmares begin. Especially when you are working with another company that controls the other side of the connection and is using a different SOAP toolkit than yours. That was no fun at all, either time that we did it.

There are many good reasons why almost everyone has abandoned SOAP in favor of REST. Here's one set of them:

https://www.quora.com/Is-REST-a-bet...ey-are-different-tools-for-different-purposes