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();
            }
        }
    }
}
7
  • 2
    and full error message Commented Oct 16, 2014 at 11:38
  • Sorry, added my code. Commented Oct 16, 2014 at 11:41
  • My crystal ball told that you probably forgot to add using System.Xml; Commented Oct 16, 2014 at 11:41
  • He seems to have several candidates, there is a conflict. Show your using statements and the entire error message. Commented Oct 16, 2014 at 11:42
  • There's actually nothing wrong with using (XmlWriter writer = XmlWriter.Create("output.xml")), I have ran a sample console application using it now. Are you missing a reference as @Reniuz mentioned? Commented Oct 16, 2014 at 11:45

5 Answers 5

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\

Sign up to request clarification or add additional context in comments.

5 Comments

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 codewriter.WriteStartElementcode stuff from the msdn example
The file will be stored in your local storage folder, e.g. "C:\Users\[name]\AppData\Local\Packages\[appName]\LocalState\"
I have updated my answer to include a link about file storage in Windows Store Apps.
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
@MortenToudahl I made a mistake in my previous comment regarding the file location. I updated it.
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();
    }

3 Comments

Thank you for your suggestiong. However, this gives me the exact same result. Are you testing in a console application?
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 stackoverflow.com/questions/24615574/…
answer provided by Patrice Gahide is absolutely right
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();

6 Comments

Thank you for your suggestiong. However, this gives me the exact same result. Are you testing in a console application?
Nope .Let me try plz and i'll tell you
I tried a console app and the build is ok.What is your framework?Visual studio?
Visual studio ultimate 2013 with resharper 8
My entire MainPage codebehind file is in my original post. Cant you get it from there?
|
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.

1 Comment

Can i rectify this? If so, how?
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.

2 Comments

Actually, i dont see a Create(String). I have .net 4.5 and 4.5.1
Thanks for the suggestion, but using (XmlWriter writer = XmlWriter.Create("output.xml", new XmlWriterSettings())) gives the same error as my original code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.