C# XmlTextWrite can't write

3559 views c#
1

I have a problem with the XmlTextWriter class, I can't write to a file.

It is a console application I'm creating and the function looks like this.

private void WriteXML()
    {
        XmlTextWriter writer = new XmlTextWriter("DataW.xml", System.Text.Encoding.UTF8);
        writer.Formatting = Formatting.Indented;
        writer.WriteStartDocument();
        writer.WriteComment("This is a comment");
        writer.WriteStartElement("Weekly Review");

        writer.WriteStartElement("DateTime");
        writer.WriteStartAttribute("09/04/2018 05:00");
        writer.WriteElementString("Activity", "At Theodor");
        writer.WriteElementString("Social", "Not Theodor,Theodor");
    }

The file I'm writing to is in the solution explorer and located the place as the file I'm reading from (which works)

answered question

What does I can't write to a file mean, specifically?

1 Answer

9

Add a using statement around your code like so:

using (XmlTextWriter writer = new XmlTextWriter("DataW.xml", System.Text.Encoding.UTF8))
{
    writer.Formatting = Formatting.Indented;
    writer.WriteStartDocument();
    writer.WriteComment("This is a comment");
    writer.WriteStartElement("Weekly Review");

    writer.WriteStartElement("DateTime");
    writer.WriteStartAttribute("09/04/2018 05:00");
    writer.WriteElementString("Activity", "At Theodor");
    writer.WriteElementString("Social", "Not Theodor,Theodor");
}

posted this

Have an answer?

JD

Please login first before posting an answer.