无法将XML写入文件

时间:2015-12-06 08:52:09

标签: c# android xml xamarin xamarin.android

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

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

1 个答案:

答案 0 :(得分:0)

在行 var storage = chrome.storage.local; chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { if (changeInfo.status == 'complete' && tab.status == 'complete' && tab.url != undefined) { storage.get('URLs', function(URLs){ for (var item in URLs) { if (tab.url == item) { return; } else { //alert ("tab load complete"); storage.set({URLs: [tab.url]}); chrome.tabs.executeScript(tab.id, { "file": "flashblocker.js" }, function () { // Execute your code console.log("Script Executed .. "); // Notification on Completion }); } } }); } }); 方法File.WriteAllText(filePath, doc.ToString());中返回doc.ToString()

而是使用System.Xml.XmlDocument属性或doc.OuterXml方法。