簡體   English   中英

將xml寫入文件

[英]Write xml to file

我知道這里有很多相同的問題,但這並不能解決我的問題

我想將xml寫入文件。 在Xamarin上編寫Android應用。

我已經創建了

我這樣創建xml:

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);

我嘗試這樣寫文件:

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

並出現此錯誤- Access to the path "/myFile.xml" is denied.

我試圖像這樣寫文件,但沒有幫助。

我在設備上看不到文件(嘗試通過資源管理器查找文件)

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

我該如何解決這個問題?

感謝幫助

似乎您正在嘗試使用無效路徑/myFile.xml保存文件。

通過執行以下操作,嘗試使用標准路徑:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM