【问题标题】:Cannot write XML to file [duplicate]无法将 XML 写入文件 [重复]
【发布时间】:2015-12-06 08:52:09
【问题描述】:

我在 C# (Xamarin) 上编写 Android 应用程序

我试图将 xml 写入文件

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

我试着这样写,但我在设备上看不到它。

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

如何将其写入文件,或者我的代码中的某些内容不正确?

【问题讨论】:

    标签: c# android xml xamarin xamarin.android


    【解决方案1】:

    在行中File.WriteAllText(filePath, doc.ToString()); 方法doc.ToString() 返回System.Xml.XmlDocument

    改为使用doc.OuterXml 属性或doc.Save(path) 方法。

    【讨论】:

    • 在哪里可以看到带有 xml 的文件?
    • 变量filePath 的代码值为“PATH_TO_USERS_FOLDER/CURRENT_USER/Documents/myFile.xml”。像“C:\Users\User\myFile.xml”(在 Windows 中)。它可能在当前用户的文件夹中(在 android 中)。
    • 但我是为 Android 手机而不是 PC 写的
    • 可以调试代码吗?
    • 是的。我调试了代码,但在控制台中没有看到我的文件正在写入。
    猜你喜欢
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多