I finally found out how to call the API - it was not easy, because of the language that i do not speak, but anyway. The service expects two parameters to be always set and they are not filled by default. The parameters are: Accept and Accept-Language. Once they are set, everything works as expected. The base URL of the service is http://api.arbetsformedlingen.se/ and the method with parameter is (for example) platsannons/matchning?kommunid=180.
One possibility to retrieve the data using the standard (available in .NET Framework 3.5) WebClient class looks like this:
try
{
// API base address
var baseUrl = @"http://api.arbetsformedlingen.se/";
// method platsannons/soklista/komunner with parameter landid set to some value
var method = string.Format("platsannons/soklista/kommuner?lanid={0}", 10);
var client = new WebClient();
// important - the service requires this two parameters!
client.Headers.Add(HttpRequestHeader.Accept, "application/xml");
client.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US");
// retrieve content
var responseContent = client.DownloadString(string.Format("{0}{1}", baseUrl, method));
// "create" the xml object
var xml = XDocument.Parse(responseContent);
// do something with the xml
xml.Root.Descendants("sokdata").ToList().ForEach(li =>
{
Console.WriteLine(string.Format("{0} - {1}", li.Element("id").Value, li.Element("namn").Value));
});
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
exception.Dump();
}
The output is:
1082 - Karlshamn
1080 - Karlskrona
1060 - Olofström
1081 - Ronneby
1083 - Sölvesborg
9090 - Ospecificerad arbetsort
For the database part - you need to decide which data you want to save and how: single values like id, name, etc. or the complete xml response at once, or something else. After you decide this, you can design your table and proceed with saving the data.
I assume by C# syntax you mean how do i:
- download something from the web
- save it to a MSSQL database
using C# with the standard .NET Framework (no third party libraries).
Let's start with the first point:
The .NET Framework has a build-in web client component called WebClient specifically for such tasks:
WebClient client = new WebClient ();
Stream data = client.OpenRead (URI);
StreamReader reader = new StreamReader (data);
string s = reader.ReadToEnd ();
...
Use the client to access and download the content. You can pass credentials and other parameters to the client, so that the request is equivalent to a browser call to the web page
How to store the result:
If the result of the web request comes in XML format, a good choice would be the LINQ to XML technology. It is very easy and intuitive to use.
Another advantage of LINQ is that it supports also access to MSSQL server, so you can very easily add the fetched data to the database using LINQ to SQL. After the DBML file is created, the code for adding data to a table using LINQ is very simple:
Data data = new Data
{
Id = 123,
Tag = "some data",
TimeStamp = DateTime.Now
// …
};
// Add data to table.
db.Data.InsertOnSubmit(data);
db.SubmitChanges();
You have also always the option of standard ADO.NET access which is very flexible and powerfull. Code can look like this:
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create the Command and Parameter objects.
SqlCommand command = new SqlCommand("insert into", connection);
command.Parameters.AddWithValue("@TagName", "some data...");
// open connection to the database
connection.Open();
SqlDataReader reader = command.ExecuteNonQuery();
}
The downside (and sometimes the advantage) in this case is that you need to take care of literally everything - from connection and command to executing and reading the result. But the option is there.
XmlReaderand map the XML data to an object. Afterwards, persist this object.