Create an xml file using 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 created a Blank Page project for a Windows Store app. In it, ive inserted example code from msdn to test how the XmlWriter class works.

VS gives me an 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 problems in a console application. But I need to make an application for the Windows store.

What I want to do is add to the existing xml file.

Can someone tell me why this does not work or how to achieve my goal differently.

Thank you for your time.

EDIT:

Sorry, my code is:

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

Windows Store, ? :

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

, .

, , Windows Store. . , - : C:\Users\[name]\AppData\Local\Packages\[appName]\LocalState\

+1

,

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

button_submit_Click .Works :

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

Visual Studio View Object Browser.

System.Xml

1. System.Xml
    - System.Xml
       - XmlWriter

XmlWriter, . Create , . Create(string), . , , , , . , .NET Framework, ​​ - .

EDIT:

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

, .

0

Because you wrote: I created the "Blank Page" project for the Windows Store app . The problem is that this method is not supported in Portable Class Library.NET for Windows Store applications . If you compare Create (stream) and Create (string) versions in the documentation, you will see that Create (string) is not supported in the selected structure.

0
source

All Articles