将xml写入文件 [英] Write xml to file

查看:39
本文介绍了将xml写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有很多相同的问题,但它并没有解决我的问题

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

我想将 xml 写入文件.在 Xamarin 上编写 Android 应用.

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

我已经创建了

我像这样创建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);

我尝试像这样写入文件:

I try to write to file like this:

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

并出现此错误 - 对路径/myFile.xml"的访问被拒绝.

我试着写这样的文件,但没有用.

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?

感谢帮助

推荐答案

您似乎正在尝试使用无效路径保存文件:/myFile.xml.

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

这篇关于将xml写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆