0

I have this link: http://www.bnro.ro/nbrfxrates.xml to an xml file with daily Currency.

I want to save this xml in folder of my website ex. (~/Content/CurrencyXml).

I want to save it every day, first time when a user is accessing my website (must replace the old file).

I want also to read it in a static method which return an object with each currency type like property, in order to access it something like this:

price=model.Eur * 100;

Can you help me with an example of how to save a copy of this xml and how to read it?

Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
Lucian Bumb
  • 2,821
  • 5
  • 26
  • 39
  • see at XDocument class. You can use Load method to download xml and Save to save it in your local folder. – chameleon Jan 29 '16 at 22:42
  • Why not create a simple windows service that performs that task once a day. There are plenty of examples on the web on how to download xml and save it i.e. WebClient, HttpWebRequest. I'm not sure how you would check this on a daily basis, but not to affect performance too much, why check if a flag file exists or not every time a user access your website and if it doesn't exists, create it. If it does, check the date & time and if over 24 hours, request the data again. – Thierry Jan 29 '16 at 22:48
  • @Thierry - Thank you for your comment, I am looking now for tutorials in order to understand how to make and use windows services . – Lucian Bumb Jan 29 '16 at 23:10

2 Answers2

1

You can try something like this to read the file and save it

public void readFile()
{
       XmlDocument document = new XmlDocument();
       document.Load("http://www.bnro.ro/nbrfxrates.xml");
       document.Save(Path.Combine(
       Server.MapPath("~/Content/CurrencyXml"),"myFile.xml"));
}

and in here you can find very usefull information:

XmlDocument Microsoft

1

Here's an example for the windows service:

Walkthrough: Creating a Windows Service Application in the Component Designer

Here's an example on how to download xml using WebClient:

How can I download an XML file using C#?

Here's an example on how to save a file to your server:

C# save files to folder on server instead of local

Then use what @Augustin suggested.

If you do use the flag file scenario, I would advice to save the xml file to a temporary file and only when fully downloaded, overwrite your original one and make sure to handle locking, overwriting time, etc... One other solution would be to use a temp variable that holds the current filename and when you download a new file, just give it a new name and when downloaded, set that variable name to the new filename and make sure that your load function uses that variable and loads the data from the new filename. Don't forget to delete your old file(s), but at least it will avoid locking issues if any and you can always try to delete the file later again.

If will very much depend if your website if being access 24/7 or not. If it isn't it is less of an issue as you could use the windows service to download this file when you know it's not being used. If you use the windows service, while being used, same as above would apply. If you're using Azure, you could use a WebJob. I'm sure there are many different solutions to handle this and you just have to find the one that will meet your needs.

Community
  • 1
  • 1
Thierry
  • 6,142
  • 13
  • 66
  • 117