Write xml to file

I know that there are many same questions here, but it doesn't solve my problem

I want to write xml to file. Writing Android app on Xamarin.

I already create it

I create xml like this:

XmlDocument doc = new XmlDocument();
XmlElement el = (XmlElement)doc.AppendChild(doc.CreateElement("Order"));
el.SetAttribute("CallConfirm", "1");
el.SetAttribute("PayMethod", "Безнал");
el.SetAttribute("QtyPerson", "");
el.SetAttribute("Type", "2");
el.SetAttribute("PayStateID", "0");
el.SetAttribute("Remark", "{Comment}");
el.SetAttribute("RemarkMoney", "0");
el.SetAttribute("TimePlan", "");
el.SetAttribute("Brand", "1");
el.SetAttribute("DiscountPercent", "0");
el.SetAttribute("BonusAmount", "0");
el.SetAttribute("Department", "");

XmlElement el2 = (XmlElement)el.AppendChild(doc.CreateElement("Customer"));
el2.SetAttribute("Login", "");
el2.SetAttribute("FIO", "{FIO}");

XmlElement el3 = (XmlElement)el.AppendChild(doc.CreateElement("Address"));
el3.SetAttribute("CityName", "{CityName}");
el3.SetAttribute("StationName", "");
el3.SetAttribute("StreetName", "{StreetName}");
el3.SetAttribute("House", "{HouseName}");
el3.SetAttribute("Corpus", "");
el3.SetAttribute("Building", "");
el3.SetAttribute("Flat", "{FlatName}");
el3.SetAttribute("Porch", "");
el3.SetAttribute("Floor", "");
el3.SetAttribute("DoorCode", "");

XmlElement el4 = (XmlElement)el.AppendChild(doc.CreateElement("Phone"));
el4.SetAttribute("Code", "{Code}");
el4.SetAttribute("Number", "{Phone}");

XmlElement el5 = (XmlElement)el.AppendChild(doc.CreateElement("Products"));

XmlElement el6 = (XmlElement)el5.AppendChild(doc.CreateElement("Product"));
el6.SetAttribute("Code", "{ProductCode}");
el6.SetAttribute("Qty", "{QTY}");

Console.WriteLine ("TUT");
Console.WriteLine(doc.OuterXml);

I try to write to file like this:

File.WriteAllText("myFile.xml",doc.ToString());

And have this error - Access to the path "/myFile.xml" is denied.

I try to write file like this, but it doesn't help.

I don't see file on device (try to find it via explorer)

 var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var filePath = Path.Combine(documentsPath, "myFile.xml");
File.WriteAllText(filePath, doc.ToString());

How I can solve this problem?

Thank's for help

Answers


It seems like you are trying to save the file with an invalid path: /myFile.xml.

Try using a fully qualified path, by doing the following:

var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var filePath = Path.Combine(documentsPath, "myFile.xml");
File.WriteAllText(filePath, doc.ToString());

Need Your Help

How can I highlight the line of text that is closest to the mouse?

javascript html text line highlight

I have a long text and I'd like to offer the user a reading help: The current line should be highlighted. To make it easier, I'll just use the Y coordinate of the mouse (this way, the mouse pointer...

Silverstripe 3.1.2 - PaginatedList only for Dataobjects related to page

php silverstripe

I want to create a paginated news section with different categories.