8

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();
            }
        }
    }
}
Morten Toudahl
  • 452
  • 1
  • 4
  • 13

5 Answers5

4

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\

Patrice Gahide
  • 3,644
  • 1
  • 27
  • 37
  • Yes. It is a windows store app. Ive only used c# for a month or so, so i didnt know it made a difference. Also, your solution seems to work. However, no file shows up. In the `code`//do stuff `code` area, i put in the `code`writer.WriteStartElement`code` stuff from the msdn example – Morten Toudahl Oct 16 '14 at 12:07
  • The file will be stored in your local storage folder, e.g. "C:\Users\\[name]\AppData\Local\Packages\\[appName]\LocalState\" – Patrice Gahide Oct 16 '14 at 12:25
  • I have updated my answer to include a link about file storage in Windows Store Apps. – Patrice Gahide Oct 16 '14 at 12:28
  • It is not there, at all. A friend just told me that it is not possible to save xml files to the root folder. And linked me to info on the StorageFolder class. I assume i will make your code work, when i fix the location it is saved. Thanks for your help – Morten Toudahl Oct 16 '14 at 12:32
  • @MortenToudahl I made a mistake in my previous comment regarding the file location. I updated it. – Patrice Gahide Oct 16 '14 at 17:51
1

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();
    }
Mysterion
  • 384
  • 4
  • 9
  • Thank you for your suggestiong. However, this gives me the exact same result. Are you testing in a console application? – Morten Toudahl Oct 16 '14 at 11:58
  • Yes, and it runs smoothly in console application , try using this, 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(); or use this link http://stackoverflow.com/questions/24615574/windows-8-store-app-xmlwriter-does-not-write-anything-to-file – Mysterion Oct 16 '14 at 12:10
  • answer provided by Patrice Gahide is absolutely right – Mysterion Oct 16 '14 at 12:27
1

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();
Giannis Grivas
  • 3,374
  • 1
  • 18
  • 38
1

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.

Renatas M.
  • 11,694
  • 1
  • 43
  • 62
0

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.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154