create xml file programmatically
im trying to create an xml file and then save it to a file location...
string xmlPath = AppDomain.CurrentDomain.Bas开发者_开发问答eDirectory.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...
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.
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.
if you are using a windows application path will be pointing to the bin directory i think it is saving in bin directory
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.
精彩评论