Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create xml file programmatically

Tags:

c#

xml

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...

like image 311
Rafay Avatar asked Jan 10 '11 09:01

Rafay


2 Answers

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.

like image 89
marc_s Avatar answered Sep 23 '22 14:09

marc_s


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.

like image 33
Darin Dimitrov Avatar answered Sep 25 '22 14:09

Darin Dimitrov