Create xml file with XmlWriter

Morten Toudahl picture Morten Toudahl · Oct 16, 2014 · Viewed 19.2k times · Source

I am having problems with the Create method in XmlWriter.

Even when i use the example from msdn, it will not work. http://msdn.microsoft.com/en-us/library/kcsse48t.aspx

I have created a "Blank page" project, for a windows store app. In it, ive inserted the example code from msdn, to test how the XmlWriter class works.

VS gives me the error:

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)

This works without a problem in a console application. But i need to make a windows store app.

What i wish to end up doing, is adding to an existing xml file.

Can someone tell me why this does not work, or how to reach my goal in a different way.

Thank you for your time.

EDIT:

Sorry, my code:

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

Answer

Patrice Gahide picture Patrice Gahide · Oct 16, 2014

This is a Windows Store App, isn't it? Edit your question tags and try this:

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

You can remove namespaces qualifiers once you know it works.

Check this to learn about how to store files in Windows Store Apps. Also take a look at this for samples. My code will use the Local App Folder, i.e. something like: C:\Users\[name]\AppData\Local\Packages\[appName]\LocalState\