tags:

views:

37

answers:

2

Hi

I am in trouble, so need some help. I have my file stored in the following path-->

H:\Testapps\appz\Sample1\Sample1\Data.xml

I am trying to load the xml file in an XMLDOCUMENT like this -->

XMLDocument xdoc=new XMLDocument(); string xmlfile = "..\Sample1\Data.xml"; xdoc.Load(xmlfile);

I get an error - Could not find a part of the path 'H:\Testapps\appz\Sample1\Sample1\bin\Debug\Sample1\Sample1\RemData.xml'.

How do i proceed in specifying the xmlfile path?? Any help appreciated!

+1  A: 

You are using a relative path. Doing so means that you're telling the application to use the executable's directory as the "root path". What you probably want is to automatically have the XML files copied to the output directory. To do this, I usually set the "Build Action" property for the XML file to "Embedded Resource" and set "Copy to Output Directory" to "Copy Always". Hope this helps.

BTW, you might also want to take a look at Pack URI's.

karmicpuppet
You don't want to bother making them an embedded resource if you're going to copy to output, do one or the other rather. If an embedded resource which is really what is probably best, then you need to use the resource stream to get to it.
Jimmy Hoffa
Thanks!You made it so simple and fast for me:)
laksh
Thanks Karmicpuppet and jimmy.
laksh
It is relative from Environment.CurrentDirectory, not the .exe location. Getting the wrong current directory, or getting it changed, still makes this solution fail. Use Assembly.Location
Hans Passant
Great points Jimmy and Hans.
karmicpuppet
Another problem i am facing is when i try to write new node in an existing xml file by providing just the name of the xml file, i do not see contents saved in the xml file.But if i provide the complete path for the xml file, the data gets saved in the aml file.I am usingxdoc.Save("RemData.xml");I want a partial file path rather than H:\Testapps\appz\Sample1\Sample1\bin\Debug\Sample1\Sample1\RemData.xml but how?
laksh
A: 

Another problem i am facing is when i try to write new node in an existing xml file by providing just the name of the xml file, i do not see contents saved in the xml file.

But if i provide the complete path for the xml file, the data gets saved in the aml file.

I am using

xdoc.Save("RemData.xml");

I want a partial file path rather than H:\Testapps\appz\Sample1\Sample1\bin\Debug\Sample1\Sample1\RemData.xml but how?

laksh
Ok, first off, note that if you do the suggested methods in the previous answer, you will actually have two copies of your XML file: the one you see in VS Solution Explorer (i.e. under "H:\Testapps\appz\Sample1\Sample1"), and the one in your output directory ("H:\Testapps\appz\Sample1\Sample1\bin\Debug\Sample1\Sample1\"). When you're running the application and you're using the "relative path", you're actually working with the XML file in the Output Directory. Thus, doing a Save on the XDocument will update that one and not the one in Solution Explorer. Hope this makes sense.
karmicpuppet
so how do i refer to the file under VS Soln Explorer ? I want to avoid providing the full path name.
laksh
you have explained it really nicely.thanks karmicpuppet.
laksh