Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2002
    Location
    Belgium, Limburg
    Posts
    7,196

    XML error: Token StartElement in state Epilog would result in an invalid XML document

    I get this error when writing a XML file, but I can't for the life of me figure out where I go wrong.

    Code:
            
    
    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                string file = @"Records.xml";
                XmlTextWriter writer = new XmlTextWriter(file, null);
    
                writer.Formatting = Formatting.Indented;
                writer.WriteStartDocument();
    
                foreach (Game game in gamesList)
                {
                    writer.WriteStartElement("Game");
                    
                    writer.WriteElementString("date", game.Date);
                    writer.WriteElementString("type", game.Type);
                    writer.WriteElementString("blinds", game.Blinds);
                    writer.WriteElementString("buyin", game.Buyin.ToString());
                    writer.WriteElementString("buyout", game.Buyout.ToString());
                    writer.WriteElementString("hoursPlayed", game.HoursPlayed);
                    writer.WriteElementString("result", game.Result.ToString());
    
                    writer.WriteEndElement();
                    
                }
    
                writer.WriteEndDocument();
                writer.Close();
            }
    As far as I can tell, I'm doing everything right. I open the document, then write an element for every game in the gamesList, then close the document after that is done... ?

    What am I missing here?
    Technical Artist at LuGus Studios
    www.lugus-studios.be


    nielsVaes.be


  2. #2
    Join Date
    Feb 2002
    Location
    Belgium, Limburg
    Posts
    7,196
    Ack, my GoogleFu skills were weak, I found the problem.

    I forgot to add a "root" element before starting to write the other elements.

    Code:
    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                string file = @"Records.xml";
                XmlTextWriter writer = new XmlTextWriter(file, null);
    
                writer.Formatting = Formatting.Indented;
                writer.WriteStartDocument();
                writer.WriteStartElement("root"); /////////////////////// FORGOT THIS ONE :)
    
                foreach (Game game in gamesList)
                {
                    writer.WriteStartElement("Game");
                    
                    writer.WriteElementString("date", game.Date);
                    writer.WriteElementString("type", game.Type);
                    writer.WriteElementString("blinds", game.Blinds);
                    writer.WriteElementString("buyin", game.Buyin.ToString());
                    writer.WriteElementString("buyout", game.Buyout.ToString());
                    writer.WriteElementString("hoursPlayed", game.HoursPlayed);
                    writer.WriteElementString("result", game.Result.ToString());
    
                    writer.WriteEndElement();
                    
                }
    
                writer.WriteEndDocument();
                writer.Close();
            }
    Technical Artist at LuGus Studios
    www.lugus-studios.be


    nielsVaes.be


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •