Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
            }
        }
    }
}
like image 402
Morten Toudahl Avatar asked Oct 16 '14 11:10

Morten Toudahl


People also ask

How read and write data from XML in C#?

The XmlReader, XmlWriter and their derived classes contains methods and properties to read and write XML documents. With the help of the XmlDocument and XmlDataDocument classes, you can read entire document. The Load and Save method of XmlDocument loads a reader or a file and saves document respectively.

What is XML Textwriting?

Represents a writer that provides a fast, non-cached, forward-only way of generating streams or files containing XML data that conforms to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations. Starting with the . NET Framework 2.0, we recommend that you use the XmlWriter class instead.

How to create XML file using XmlWriter in c#?

It contains the XML declaration and a single XML tag. var sts = new XmlWriterSettings() { Indent = true, }; using XmlWriter writer = XmlWriter. Create("data. xml", sts);

What is the difference between XmlWriter and XmlTextWriter?

XmlWriter is an abstract class. XmlTextWriter is a specific implementation of XmlWriter .


3 Answers

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\

like image 179
Patrice Gahide Avatar answered Sep 25 '22 05:09

Patrice Gahide


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();
    }
like image 45
Mysterion Avatar answered Sep 22 '22 05:09

Mysterion


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();
like image 31
Giannis Grivas Avatar answered Sep 24 '22 05:09

Giannis Grivas