Mor*_*ahl 7 c# xml xmlwriter windows-store-apps
我在XmlWriter中遇到了Create方法的问题.
即使我使用msdn中的示例,它也无法正常工作. http://msdn.microsoft.com/en-us/library/kcsse48t.aspx
我为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)
Run Code Online (Sandbox Code Playgroud)
这在控制台应用程序中没有问题.但我需要制作一个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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是 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();
}
}
Run Code Online (Sandbox Code Playgroud)
一旦您知道命名空间限定符有效,您就可以删除它。
检查此内容以了解如何在 Windows 应用商店应用程序中存储文件。另请查看示例。我的代码将使用本地应用程序文件夹,即类似:C:\Users\[name]\AppData\Local\Packages\[appName]\LocalState\
| 归档时间: |
|
| 查看次数: |
12914 次 |
| 最近记录: |