1

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!


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?

1 Answer 1

2

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.

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

5 Comments

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.
Thanks! You made it so simple and fast for me:)
Thanks Karmicpuppet and jimmy.
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
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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.