2

I have a url address in order to retrieve API data and if I paste the URL address into a textbox at a browser and then press enter, it ask if I wanna save it. The format is XML.

My request is to use C# to retrieve the API data and save it into a SQL server's table.

The obstacle I'm is what C# syntax do I need to use in order to do it? Any advice?

No third person application or Add-in to Visual Studio 2008.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Net;


namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {


            string uri = "http://api.arbetsformedlingen.se/platsannons/matchning?kommunid=180&nyckelord=bagare";
            var result = "";
            try
            {
                using (WebClient webClient = new WebClient())
                {
                    using (var stream = webClient.OpenRead(uri))
                    {
                        using (var streamReader = new StreamReader(stream))
                        {
                            result = streamReader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var wtf = ex.Message;
            }


        }
    }
}
4
  • Use an XmlReader and map the XML data to an object. Afterwards, persist this object. Commented Nov 21, 2013 at 22:01
  • if you save the data, it will ask about xml. In the beginning of the prcess it does not ask about xml. Please correct me if I have wrong. Commented Nov 21, 2013 at 22:08
  • 1
    I have no clue what it is you're trying to say. Commented Nov 21, 2013 at 22:10
  • The URL address do not ask about xml. A similiar address is google's API. "maps.googleapis.com/maps/api/…". Inside of my URL address (not google address) contains password and login. In other words, you use the URL address, with pwd and login, to ask a request to retrieve the list. After asking for the list, the browser ask you if you wanna save the file as xml format. Please tell me if you need more information. Commented Nov 21, 2013 at 22:16

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help! I strongly that I need more support. If you have the API link "api.arbetsformedlingen.se/platsannons/…" and I tried to follow your instruction but I retrieve alot of bug error.
Thank you for your help and support!!! This solution will help a lot user in my situation. It was an complicated code you have created. I have a question if it possible to do it. Is there an approach to change the syntax code "MediaTypeWithQualityHeaderValue" and "httpClient" into another and similiar syntax code that is similiar in .net framwork 3.5? In this context, I'm using .net framework 3.5.
I have updated the code in my answer, so that only .NET Framework 3.5 classes are used.
0

HttpClient to load the xml data http://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx

Then parse into a XmlDocument, process as needed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.