0

I am trying to read a xml file using c#

XmlDocument doc = new XmlDocument();
doc.Load(@"/Rules/AssessmentRule.xml");
XmlNode node = doc.SelectSingleNode("/RuleName");
string URI = node.InnerText;
return URI;

I kept breakpoints in 2nd and 3rd line. I get error in the line below

doc.Load(@"/Rules/AssessmentRule.xml");

It says

Could not find a part of the path 'C:\Program Files\Rules\AssessmentRule.xml'.

The folder structure of my project is, it is having the Rules folder in the same place as my class file

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150

5 Answers5

2

When running in debug the path is based from Debug settings, which defaults to bin\debug unless you access the file with full path it will be relative to that folder(bin\debug). [courtesy @miltonb]

so below are two solutions.

you can add that file into your VS project. then click on that file in VS go to properties set 'Copy to output directory' -> copy always. then just need to give the file name

or you get your project directory like this

string projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
string xmlLocation = @"Rules/AssessmentRule.xml";
String fullPath = Path.Combine(projectPath,xmlLocation);
Muhammad Zaighum
  • 560
  • 4
  • 21
  • Path will not be combined until you remove leading forward slash `@"Rules/AssessmentRule.xml";` – Hassan May 28 '14 at 06:25
  • It seems odd that you have the same text at the top of your answer as my answer but your timestamp is later. Suspicious. – miltonb May 28 '14 at 06:30
  • its seems very odd when you vote down a very detailed answer :) we are here to help others. I will not vote down you because your answer is not wrong – Muhammad Zaighum May 28 '14 at 06:36
  • I like your answer and would have upvoted had you had courtesy to refer to my answer. It would have been nice. You do have the best answer so far. – miltonb May 28 '14 at 06:41
  • My mistake.. does that make sense now ? – Muhammad Zaighum May 28 '14 at 06:43
  • 1
    the path is not based on "bin\debug" per se, it's based on the Working directory in the Debug settings, which *defaults* to bin\debug (if running in debug mode). – default May 28 '14 at 07:19
2

If the file in your project folder Try this code for path

string wanted_path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirect‌​ory()));

then find the file on that path.

sagar43
  • 3,341
  • 3
  • 29
  • 49
0

When running in debug the path is based from 'bin\debug' unless you access the file with full path it will be relative to that folder.

miltonb
  • 6,905
  • 8
  • 45
  • 55
  • the path is not based on "bin\debug" per se, it's based on the Working directory in the Debug settings, which defaults to bin\debug. Also, I'm not sure how this answers the OPs question. – default May 28 '14 at 07:19
0

You need to get the project directory, then append the xml path.

string projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
string xmlLocation = @"Rules/AssessmentRule.xml";
String fullPath = Path.Combine(projectPath,xmlLocation);
JuStDaN
  • 449
  • 2
  • 7
  • Path will not be combined until you remove leading forward slash `@"Rules/AssessmentRule.xml";` – Hassan May 28 '14 at 06:26
0

You should probably use an OpenFileDialog instead. It'll make your life a lot easier:

var openFile = new Microsoft.Win32.OpenFileDialog() {
    CheckFileExists = true,
    CheckPathExists = true,
    Filter = "XML File|*.xml"
};

if (openFile.ShowDialog() ?? false)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(openFile.FileName);
    XmlNode node = doc.SelectSingleNode("/RuleName");
    string URI = node.InnerText;

    return URI;
}
else
{
    // User clicked cancel
    return String.Empty;
}
Fireboyd78
  • 103
  • 7
  • That's not correct - the program should be able to open files without user interaction, which I believe that the OP is trying to do. – default May 28 '14 at 07:23