C# 使用XmlWriter创建xml文件

C# 使用XmlWriter创建xml文件,c#,xml,windows-store-apps,xmlwriter,C#,Xml,Windows Store Apps,Xmlwriter,我对XmlWriter中的Create方法有问题 即使我使用msdn中的示例,它也不会起作用。 我已经为windows应用商店应用程序创建了一个“空白页”项目。 在其中,我插入了来自msdn的示例代码,以测试XmlWriter类的工作方式 VS给了我一个错误: Cannot resolve method 'Create(string)', candidates are: System.Xml.XmlWriter Create(System.IO.Stream) (in class XmlWr

我对XmlWriter中的Create方法有问题

即使我使用msdn中的示例,它也不会起作用。

我已经为windows应用商店应用程序创建了一个“空白页”项目。 在其中,我插入了来自msdn的示例代码,以测试XmlWriter类的工作方式

VS给了我一个错误:

Cannot resolve method 'Create(string)', candidates are: 
System.Xml.XmlWriter Create(System.IO.Stream) (in class XmlWriter)
System.Xml.XmlWriter Create(System.IO.TextWriter) (in class XmlWriter)
System.Xml.XmlWriter Create(System.Text.StringBuilder) (in class XmlWriter)
System.Xml.XmlWriter Create(System.Xml.XmlWriter) (in class XmlWriter)
这在控制台应用程序中可以正常工作。但我需要制作一个windows应用商店应用程序

我希望最后做的是添加到现有的xml文件中

有人能告诉我为什么这不起作用,或者如何以不同的方式实现我的目标吗

谢谢你抽出时间

编辑:

对不起,我的代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Xml;
using System.Xml.Linq;
using Windows.Data.Xml.Dom;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace DatabaseTest
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }


        private void button_submit_Click(object sender, RoutedEventArgs e)
        {
            using (XmlWriter writer = XmlWriter.Create("output.xml"))
            {
                writer.WriteStartElement("book");
                writer.WriteElementString("price", "19.95");
                writer.WriteEndElement();
                writer.Flush();
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用System.Xml;
使用System.Xml.Linq;
使用Windows.Data.Xml.Dom;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
//空白页项模板被记录在http://go.microsoft.com/fwlink/?LinkId=234238
命名空间数据库测试
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
私有无效按钮\u提交\u单击(对象发送者,路由目标)
{
使用(XmlWriter=XmlWriter.Create(“output.xml”))
{
作者。书面声明(“书”);
WriteElementString(“价格”,“19.95”);
writer.writeedelement();
writer.Flush();
}
}
}
}
这可能会有帮助

 using (XmlWriter writer = XmlWriter.Create("employees.xml"))
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("Employees");

        foreach (Employee employee in employees)
        {
        writer.WriteStartElement("Employee");

        writer.WriteElementString("ID", employee.Id.ToString());   // <-- These are new
        writer.WriteElementString("FirstName", employee.FirstName);
        writer.WriteElementString("LastName", employee.LastName);
        writer.WriteElementString("Salary", employee.Salary.ToString());

        writer.WriteEndElement();
        }

        writer.WriteEndElement();
        writer.WriteEndDocument();
    }
使用(XmlWriter=XmlWriter.Create(“employees.xml”))
{
writer.WriteStartDocument();
编写人、书面声明(“员工”);
foreach(员工中的员工)
{
编写人。书面声明(“员工”);

writer.WriteElementString(“ID”,employee.ID.ToString());//尝试在您的按钮内使用“提交”\u单击方法,但没有这样做。对我有用:

XmlWriter writer = XmlWriter.Create("output.xml"));
writer.WriteStartElement("book");
writer.WriteElementString("price", "19.95");
writer.WriteEndElement();
writer.Flush();

这是一个Windows应用商店应用程序,不是吗?编辑您的问题标签,然后尝试以下操作:

StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("output.xml", CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
  System.IO.Stream s =  writeStream.AsStreamForWrite();
  System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
  settings.Async = true;
  using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(s, settings))
  {
    // Do stuff...

    await writer.FlushAsync();
  }
}
一旦知道名称空间限定符有效,就可以删除它


检查以了解如何在Windows应用商店应用程序中存储文件。同时查看示例。我的代码将使用本地应用程序文件夹,例如:
C:\Users\[name]\AppData\Local\Packages\[appName]\LocalState\

在Visual Studio中,转到“视图”菜单并选择“对象浏览器”

在左侧窗格中查找
System.Xml
,并导航以下内容

1. System.Xml
    - System.Xml
       - XmlWriter
现在单击
XmlWriter
,右侧窗格将显示该类型的所有方法。查找
Create
方法并查看哪些重载可用。您应该看到
Create(string)
,就像我一样。如果不是,那么老实说,我不确定这里发生了什么。可能是因为您使用了不同的.NET框架,比如可移植类库之类的东西

编辑:

你可以用

使用(XmlWriter=XmlWriter.Create(“output.xml”,新的XmlWriterSettings())

所以至少你可以继续你的工作。

因为你写道:我为一个windows应用商店应用程序创建了一个“空白页”项目。

问题在于,Windows应用商店应用的便携式类库.NET中不支持该方法。如果比较文档中的版本信息,您将看到创建(字符串)所选框架中不支持。

和完整错误消息抱歉,添加了我的代码。我的水晶球告诉我,您可能忘记使用System.Xml添加
他似乎有几个候选项,存在冲突。显示您的using语句和整个错误消息。使用
实际上没有问题(XmlWriter=XmlWriter.Create(“output.xml”))
,我现在使用它运行了一个示例控制台应用程序。您是否缺少@Reniuz提到的参考?谢谢您的建议。但是,这给了我完全相同的结果。您是否在控制台应用程序中进行测试?不。让我试试plz,我会告诉您我尝试了一个控制台应用程序,并且构建正常。您的框架是什么?Visual studio?ViVisual studio ultimate 2013 with resharper 8My整个主页代码隐藏文件都在我的原始帖子中。你能从那里得到它吗?谢谢你的建议。但是,这给了我完全相同的结果。你是否在控制台应用程序中测试?是的,它在控制台应用程序中运行平稳,请尝试使用这个,XmlTextWriter w=new XmlTextWriter(“test.xml”,Encoding.UTF8);w.writeStarteElement(“根”);w.WriteAttributeString(“xmlns”,“x”);w.writeStarteElement(“项1”);w.WriteEndElement();w.writeStarteElement(“项2”);w.WriteEndElement();或者使用Patrice Gahide提供的此链接答案绝对正确是的。这是一个windows应用商店应用程序。我只使用c#一个月左右,所以我不知道它有什么不同。此外,您的解决方案似乎有效。但是,没有显示任何文件。在
code
//do stuff
code
区域,我输入了
code
编写器。WriteStartElement
code
来自msdn示例的内容该文件将存储在本地存储文件夹中,例如“C:\Users\[name]\AppData\local\Packages\[appName]\LocalState”我已经更新了我的答案,添加了一个关于Windows应用商店应用程序中文件存储的链接。它根本不存在。一位朋友刚刚告诉我,无法将xml文件保存到根文件夹。我还将我链接到了StorageFolder类的信息。我想在修复保存位置后,我会让你的代码正常工作。谢谢你的帮助