Jason

Jason

  • NA
  • 6
  • 0

Problem with xmltextwriter Data at the root level is invalid. Line 1, position 1.

Jul 29 2010 2:18 AM

I am getting the error "Data at the root level is invalid. Line 1, position 1." when I try to load an xml file that I create. I know why the error is being thrown. The problem is the root node is not being written to the xml file correctly. I think I maybe doing something wrong but all of the examples I have looked at seems like I am creating the xml file right. Here is my code:
 

//Check if the file exists so we do not override an existing file if (!File.Exists(fileName)) { xmlTextWriter = new XmlTextWriter(fileName, null); //Add the root node to the document if (startNode != null) { xmlTextWriter.WriteStartDocument(); xmlTextWriter.WriteStartElement(startNode); xmlTextWriter.WriteEndElement(); xmlTextWriter.WriteEndDocument(); } xmlTextWriter.Flush(); xmlTextWriter.Close(); }  

Here is an example of what is created after the xmltextwriter is closed:
 

 <?xml version="1.0"?><Projectiles />

I am not sure why the root tag is being written like it is.
Thank you for any help!

Answers (5)