0

I am writing a Xml file, however I have a while loop that asks the user to insert some data. The problem I am having is that when the user closes the command prompt box ( to input the data) the root node doesn't close itself.

How can I close the root node "People" when the user exits?

thanks

** I am using c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Text;

namespace XML
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlTextWriter writer = new XmlTextWriter("C:\\Users\\gghossei\\Desktop\\XmlTestDoc.xml", Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            // root
            writer.WriteStartElement("People");
            writer.Flush();
            bool done = false;
            while (!done)
            {
                string message;
                Console.WriteLine("Please enter your message:");
                message = Console.ReadLine();
                writer.WriteElementString("Something", message);
                writer.Flush();

            }
writer.WriteEndElement();


        }
    }

the problem is when you close this while loop by pressing x on the command prompt it doesn't close the root node "People"...

George
  • 147
  • 1
  • 15
  • What language are you using? C#? VB.NET? Java? Something else? – Tim May 25 '16 at 17:55
  • Hi tim i am using c# – George May 25 '16 at 17:56
  • 1
    Please tag you question properly, and include the code. – Tim May 25 '16 at 17:57
  • You don't include your full code, so it's not clear exactly what your problem is. Please include a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) that demonstrates your problem, ideally as a console application, that we could copy and debug. (For instance, where are you calling `XmlWriter.WriteEndElement()` for the root element?) – dbc May 25 '16 at 18:29
  • I am after the while loop, however if you exit the command prompt how will it close the root? – George May 25 '16 at 18:33
  • Here I gave you a similar example.. – George May 25 '16 at 18:41
  • 1
    That is terminating your app. YOu would either have to write a handler to trap that, or let the user enter a special keyword to indicate "end of my entry". – OldProgrammer May 25 '16 at 18:44
  • There's not function that would trap that it's closing? – George May 25 '16 at 18:45
  • Possibly see [.NET Console Application Exit Event](https://stackoverflow.com/questions/1119841/net-console-application-exit-event). – dbc May 25 '16 at 18:51
  • I'm not figuring this out.. it's not letting me.. there is no other way? – George May 25 '16 at 19:20

0 Answers0