1

I have a problem. Not very big but didn't find any answer.

I want to sore some key value pair in file Data.xml and I have saved it at root level. When I am trying to use code below.

XDocument xmlDoc = new XDocument();
xmlDoc.Load("Data.xml");

Instead of checking at project root level it is checking for some other location. I tried current directory and environment.current path. But it didn't help. I don't want to specify full path. Because when application will go live I don't want to change it. I need to use data.xml so that if values got changed we will just replace the data.xml so get new values.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Sushil Gupta
  • 97
  • 1
  • 12

2 Answers2

2

It's looking in the current directory that the application is running (bin\release or bin\debug). What you want to do is make sure that you have that XML file in your project set to Copy to Output Directory

You may want to prefix the name parameter with Application.StartupPath() for rigidity.

Community
  • 1
  • 1
SQLMason
  • 3,275
  • 1
  • 30
  • 40
  • 1
    ...and make sure you include it in your project to begin with. – Jerod Venema Dec 24 '13 at 23:53
  • 1
    Can you define, "it didn't work"? I see you got your answer elsewhere. My answer solves the problem as I interpreted your question, maybe there was information missing from the question. – SQLMason Dec 26 '13 at 17:37
  • As @JohnSaunders pointed out, I wish we would have known that this was ASP.NET. – SQLMason Dec 27 '13 at 13:59
1

I got my answer from another post. Click here

Below code can be used.

string dirpath = System.Web.HttpContext.Current.Server.MapPath("~/Data.xml");

XDocument xmlDoc = new XDocument();
xmlDoc.Load(dirpath);
Community
  • 1
  • 1
Sushil Gupta
  • 97
  • 1
  • 12