【发布时间】:2016-01-18 13:58:11
【问题描述】:
我正在编写 iOS 应用程序。
我尝试创建 xml 并将其写入文件。
我用下面的代码来做。
var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var filePath = System.IO.Path.Combine(documentsPath, "myFile.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", "1");
el.SetAttribute ("PayStateID", "0");
el.SetAttribute ("Remark", "{StreetName} , ..");
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"));
Console.WriteLine ("TUT");
//File.WriteAllText(filePath, doc.OuterXml);
//doc.Save ("myfile.xml");
doc.Save (filePath);
Console.WriteLine (doc.OuterXml);
Console.WriteLine (filePath);
当我启动我的应用程序时,它崩溃了。
我认为是因为我创建 xml 文件的代码错误。
如何正确写法?
【问题讨论】:
-
它在哪里“崩溃”,你得到什么异常? minimal reproducible example 会很有帮助 - 我们几乎可以肯定不需要您的所有代码。
-
我在模拟器上没有出现异常,但它在设备上崩溃了。我在 Xamarin Insights 上有它。 'SIGABRTCrash in System_IO_FileStream__ctor_string_System_IO_FileMode_System_IO_FileAccess_System_IO_FileShare_int_bool_System_IO_FileOptions System.UnauthorizedAccessExceptionAccess 路径“/private/var/mobile/Containers/Bundle/Application/0EC54B81-8847-46C2-AE8B-CE68F8EA17AC/murakami_kiev.app/myfile.xml”被拒绝@CharlesMager
标签: c# ios xml xamarin.android