2014-10-16 93 views
4

我遇到了XmlWriter中的Create方法问题。使用XmlWriter创建xml文件

即使当我使用msdn的例子,它将无法正常工作。 http://msdn.microsoft.com/en-us/library/kcsse48t.aspx

我为Windows应用商店应用程序创建了一个“空白页面”项目。 其中,ive插入了来自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(); 
      } 
     } 
    } 
} 
+2

显示您的代码。 – 2014-10-16 11:36:32

+2

和完整的错误信息 – Vladimirs 2014-10-16 11:38:03

+0

对不起,添加了我的代码。 – 2014-10-16 11:41:14

回答

1

这是一个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(); 
    } 
} 

一旦你知道它的作品,你可以删除命名空间限定符。

检查this了解如何在Windows应用商店应用中存储文件。还可以看看this的样品。我的代码将使用本地app文件夹,即是这样的:C:\Users\[name]\AppData\Local\Packages\[appName]\LocalState\

+0

是的。这是一个Windows应用商店。我只用了一个月左右的C#,所以我不知道它有所作为。此外,您的解决方案似乎工作。但是,没有文件显示。 在'code' //做东西'代码'区域,我把'code'writer.WriteStartElement'code'填入msdn示例中 – 2014-10-16 12:07:55

+0

这个文件将被保存在你的本地存储文件夹中,例如: “C:\ Users \\ [name] \ AppData \ Local \ Packages \\ [appName] \ LocalState \” – 2014-10-16 12:25:20

+0

我已更新我的答案,在Windows应用商店应用中包含有关文件存储的链接。 – 2014-10-16 12:28:16

0

这可能是有帮助的,

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(); 
    } 
+0

谢谢你的建议。但是,这给了我完全相同的结果。你在控制台应用程序中测试? – 2014-10-16 11:58:46

+0

是的,它在控制台应用程序中运行平稳,尝试使用它,XmlTextWriter w = new XmlTextWriter(“test.xml”,Encoding.UTF8); w.WriteStartElement(“root”); w.WriteAttributeString(“xmlns”,“x”); w.WriteStartElement(“item1”); w.WriteEndElement(); w.WriteStartElement(“item2”); w.WriteEndElement();或使用此链接http://stackoverflow.com/questions/24615574/windows-8-store-app-xmlwriter-does-not-write-anything-to-file – LostCoder 2014-10-16 12:10:32

+0

“无法解析符号XmlTextWriter” – 2014-10-16 12:19:03

0

试图将button_submit_Click方法一点点改变这样的无using内使用。适用于我:

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

谢谢你的建议。但是,这给了我完全相同的结果。你在控制台应用程序中测试? – 2014-10-16 11:57:39

+0

不。让我尝试PLZ,我会告诉你 – 2014-10-16 11:59:42

+0

我试过一个控制台应用程序,并且构建是OK。你的框架是什么?Visual Studio? – 2014-10-16 12:04:00

0

在Visual Studio中,到了查看菜单,然后选择对象浏览器

在左侧窗格中查找System.Xml,导航下面

1. System.Xml 
    - System.Xml 
     - XmlWriter 

现在点击XmlWriter和右窗格中会显示所有该类型的方法。查找Create方法并查看可用的过载。像我一样,你应该看到Create(string)。如果没有,那么老实说,我不确定这里发生了什么。这可能是因为你正在使用不同的.NET Framework,如可移植类库或什么?

编辑:

你可以只使用

using (XmlWriter writer = XmlWriter.Create("output.xml", new XmlWriterSettings()))

这样至少你可以随身携带你的工作。

+0

其实,我没有看到创建(字符串)。我有.net 4.5和4.5.1 – 2014-10-16 12:14:07

+0

感谢您的建议,但'使用(XmlWriter作家= XmlWriter.Create(“output.xml”,新的XmlWriterSettings()))'给出了与我的原始代码相同的错误 – 2014-10-16 12:20:11

0

因为你写道:我创建了一个“空白页” 项目,为Windows商店应用。 问题是该方法不支持可移植类库.NET for Windows应用商店应用。如果您在文档中比较Create(Stream)Create(string)版本信息,您将看到Create(string)在您选择的框架中不受支持。

+0

我可以纠正这个?如果是这样,怎么样? – 2014-10-16 12:15:07