Windows 8 storage application XmlWriter does not write anything to a file

I'm not very good at .NET XML libraries and trying to wrap my head around the IXmlSerializable interface. I am developing an application for the Windows Store (W8) and want to use the IXmlSerializable interface, however I am encountering problems. XmlTextWriter is not available in window-storage applications, so I use the XmlWriter factory method, but my code does not output anything to the XML file (its space).

If I use XmlSerializer, the XML file is output as expected, but wants to implement IXmlSerializable, because in my working application I will have private fields.

Here is an example of the code I'm using

public class XMLTest : IXmlSerializable
{
    private string name;
    public string Name { get { return this.name; } set { this.name = value; } }

    private XMLTest() { }

    public XMLTest(string name)
    {
        this.name = name;
    }

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        throw new NotImplementedException();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteAttributeString("Name", this.name);
    }
}

In my XAML code for

private async void saveButton_Click(object sender, RoutedEventArgs e)
    {
        XMLTest t = new XMLTest("test name");

        StorageFolder folder = ApplicationData.Current.LocalFolder;
        Stream fs = await folder.OpenStreamForWriteAsync("testingXML.xml", CreationCollisionOption.ReplaceExisting);
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.ConformanceLevel = ConformanceLevel.Auto;
        var x = XmlWriter.Create(fs, settings);

        //XmlSerializer xs = new XmlSerializer(t.GetType());
        //xs.Serialize(fs, t);

        t.WriteXml(x);
        fs.Dispose();
    }
0
1

- . fs (, ), x. using , :

using (var stream = await folder.OpenStreamForWriteAsync(...))
{
    var settings = new XmlWriterSettings { ConformanceLevel = ConformanceLevel.Auto };
    using (var writer = XmlWriter.Create(stream, settings))
    {
        t.WriteXml(writer);
    }
}
+1

All Articles