创建文件并在其中写入xml(C#)

创建文件并在其中写入xml(C#),c#,xml,win-universal-app,C#,Xml,Win Universal App,我正在为Windows10Mobile编写UWP应用程序 我创建了如下xml: XmlDocument doc = new XmlDocument(); XmlElement el = (XmlElement)doc.AppendChild(doc.CreateElement("Order")); el.SetAttribute("CallConfirm", "1"); el.SetAttribute("PayMethod", "");

我正在为Windows10Mobile编写UWP应用程序

我创建了如下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", "");
        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"));
我想创建.xml文件并将此xml写入其中

我试着像这样保存它
doc.save(“data.xml”)但有此错误

Error   CS1503  Argument 1: cannot convert from 'string' to 'System.IO.Stream'  Murakami    
我怎样才能做到这一点


非常感谢你的帮助

因为您正在使用windows universal app

您需要使用以下代码将文件保存到存储器中:

StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
await doc.SaveToFileAsync(file);

由于您正在编写Windows 10通用应用程序,
XmlDocument.Save(string)
不可用。相反,使用

    using (FileStream fs = new FileStream("test.xml", FileMode.Create))
    {
        doc.Save(fs);
    }

您是否尝试搜索如何保存
XmlDocument
?你会得到很多结果的!@CharlesMager的可能副本它是一个通用应用程序@ББГБСССССММцццццццццццццццццццц请看这个解决方案。在UWP中,这种方法可以帮助您保存xml文件。这个解决方案有效吗?对我来说,这是给未经授权的访问错误嗨,我只是想提一下,我在ubuntu 16.04中使用core net 1.0时,出现了相同的错误。但是,当我这样做时,我保留了xml,非常感谢