c# - 创建文件并在那里写入 xml (C#)

标签 c# xml win-universal-app

我正在为 Windows 10 移动版编写 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 10 通用应用程序,因此 XmlDocument.Save(string) 不可用。相反,使用

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

关于c# - 创建文件并在那里写入 xml (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37065972/

相关文章:

c# - 如何使用 C# 下载 XML 文件?

c# - 使用来自 c# 的参数运行 cmd.exe

xml - XPath 递归 "parent"- 平面结构中的选择

c# - 支持两个主窗口的通用 Windows 平台 (UWP) 应用程序

c# - 提交后清除所有字段

c# - VS 2008 调试 : System. ArgumentException?

自定义 thymeleaf 标签的 XML 命名空间

swift - 快速解析 XML 数据

c# - 在C#中更改属性后,是否有一种简单的方法可以清空文本框?

c# - 在后面的 C# 代码中设置网格列或网格行