5

im trying to create an xml file and then save it to a file location...

string xmlPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "cities.xml";

XDocument doc = new XDocument(  
        new XElement("Cities",
            new XElement("City",
                new XAttribute("id", gid),
                new XElement("CityName", cityname))));
doc.Save(xmlPath);

the problem is that its not being saved to the location specified...

4
  • Have you tried debugging it to check what the actual path is that it is given when it tries to save? Also do you get any errors, or is the file simply not where you expect it? Commented Jan 10, 2011 at 9:10
  • Are there any errors or outputs? Did you check if the path in xmlPath is valid? Did you close / stop the application before looking for the file? Commented Jan 10, 2011 at 9:10
  • you may check the xmlPath variable before calling the save to see what value it got, the default AppDomain.CurrentDomain.BaseDirectory.ToString() = the debug folder of the application Commented Jan 10, 2011 at 9:10
  • @Adkins && Martin nope im not getting any errors... @Remon yes i have set its property to copy always Commented Jan 10, 2011 at 9:13

4 Answers 4

8

Try to use the System.IO.Path.Combine method to make sure you a) have the necessary backslash between the directory and the file name, and to b) make sure you don't have multiple of those:

string xmlPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
                                        "cities.xml");

Also: maybe your user account doesn't have the permissions to write to that directory. Try using something like Isolated Storage or some other directory you're 100% sure that the user is allowed to write to.

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

Comments

3

The code looks fine and when I tested it locally it worked. Make sure that xmlPath points to a directory where the current user has write permissions. As a side note it would be better to use Path.Combine.

Comments

2

if you are using a windows application path will be pointing to the bin directory i think it is saving in bin directory

Comments

1

The best thing you can do is run this program through the debugger and check what the location is that is give in xmlpath variable, and also check if a normal local user has write permissions to this directory. There could be any number of problems with the folder or the path that is given. Without any other information however it is hard to give a more descript answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.