Morten Toudahl
Morten Toudahl

Reputation: 640

Create xml file with XmlWriter

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

Upvotes: 8

Views: 30637

Answers (5)

Patrice Gahide
Patrice Gahide

Reputation: 3754

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\

Upvotes: 4

Renatas M.
Renatas M.

Reputation: 11820

Because you wrote: I have created a "Blank page" project, for a windows store app. The problem is that method is not supported in Portable Class Library .NET for Windows Store apps. If you compare Create(Stream) and Create(string) version information in documentation you'll see that Create(string) is not supported in your selected framework.

Upvotes: 1

Jason Evans
Jason Evans

Reputation: 29186

In Visual Studio, got to the View menu and choose Object Browser.

Look for System.Xml in the left hand pane, and navigate the following

1. System.Xml
    - System.Xml
       - XmlWriter

Now click on XmlWriter and the right pane will show all the methods for that type. Look for the Create method and see what overloads are available. You should see Create(string), like I do. If not, then quite honestly I'm not sure what's happening here. It could be that you're using a different .NET Framework like Portable Class Library or something???

EDIT:

You could just use

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

so at least you can carry on your work.

Upvotes: 0

Giannis Grivas
Giannis Grivas

Reputation: 3412

Try to use inside your button_submit_Click method a little bit changed like this without using.Works for me:

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

Upvotes: 1

Mysterion
Mysterion

Reputation: 384

This might be helpful,

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

Upvotes: 1

Related Questions