Token StartElement in state Epilog would result in an invalid XML Document

Was trying to create an XML document and kept running into this error.
Exception: Thrown: "Token StartElement in state Epilog would result in an invalid XML Document." (System.InvalidOperationException) A System.InvalidOperationException was thrown: "Token StartElement in state Epilog would result in an invalid XML Document."

First check on the structure of your XML documents seems there aren't any syntax error. But if you notice the circled error in the above picture, you will realize that the problem is simply because "root" element was not included.

So make sure you always have a root element.

Attached below are sample code for creating a XML document.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlTextWriter textWriter = new XmlTextWriter(D:\\myXmFile.xml, null);
            // Opens the document
            textWriter.WriteStartDocument();
            {
                textWriter.WriteStartElement("root"); // Write root IMPORTANT

                textWriter.WriteComment("Definition of simple and complex elements");
                textWriter.WriteStartElement("person");
                textWriter.WriteStartElement("name");
                textWriter.WriteString(p.Name);
                textWriter.WriteEndElement();
                textWriter.WriteEndElement();

                textWriter.WriteEndElement();
            }
            // Ends the document.
            textWriter.WriteEndDocument();
            // close writer
            textWriter.Close();

            FormatXMLDoc();
        }
    }
}

Comments

Popular posts from this blog

225019099301.apps.googleusercontent.com

Backup MySQL to Azure Storage in 30 Seconds

Generate GoDaddy SSL Certificate (.crt) for Azure Websites (.pfx)