Tina Thomas
posted
9 posts
since
Jul 12, 2010
from
|
|
Re: Problem with xmltextwriter Data at the root level is invalid. Line 1, position 1.
|
|
|
|
|
|
|
|
|
|
|
The XML that is generated is a perfectly formatted XML and there should no be problem with this XML. It is generated as expected from the code you have pasted. You haven't mentioned how you are trying to load it. Are you loading using XmlDocument?
|
|
|
|
|
|
Jason
posted
3 posts
since
Jun 10, 2008
from
|
|
Re: Problem with xmltextwriter Data at the root level is invalid. Line 1, position 1.
|
|
|
|
|
|
|
|
|
|
Yes I am trying to load using XMLDocument and that is where I get the error. When I use the WriteStartElement(node) and WriteEndElement() I was expecting to get something like this:
<?xml version="1.0"?><Projectiles></Projectiles>
So from what you are saying, this:
<?xml version="1.0"?><Projectiles />
is ok? I thought tags had to have a starting and ending tag. Also what kind of tag is <Projectiles />?
Here is my Code:
private XmlDocument xmlDocument;
private XmlTextWriter xmlTextWriter;
private string fileName;
public Serialize(string fileName, string startNode)
{
this.fileName = fileName;
xmlDocument = new XmlDocument();
//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();
}
xmlDocument.LoadXml(fileName);
}
|
|
|
|
|
|
Tina Thomas
posted
9 posts
since
Jul 12, 2010
from
|
|
Re: Problem with xmltextwriter Data at the root level is invalid. Line 1, position 1.
|
|
|
|
|
|
|
|
|
|
|
I see, you used XmlDocument.LoadXml which accepts a string. So your file name is considered as the xml string and hence you see the error Data at the root level is invalid. To solve your problem, use
xmlDocument.Load(fileName);
or
you can read the contents of the file and use xmlDocument.LoadXml
StreamReader sw = new StreamReader(fileName); string sXml= sw.ReadToEnd(); XmlDocument oXDoc = new XmlDocument(); oXDoc .LoadXml(sXml);
|
|
|
|
|
|
Jason
posted
3 posts
since
Jun 10, 2008
from
|
|
Re: Problem with xmltextwriter Data at the root level is invalid. Line 1, position 1.
|
|
|
|
|
|
|
|
|
|
|
Thank you very much Tina!!! I used the load method and it worked out great. But I still don't understand why the WriteStartElement and WriteEndElement produces:
<?xml version="1.0"?><Projectiles />
I would have thought that it would have created the following:
<?xml version="1.0"?><Projectiles></Projectiles>
is the <Projectiles /> mean there are no elements in the root? Thanks again!!!
|
|
|
|
|
|
Tina Thomas
posted
9 posts
since
Jul 12, 2010
from
|
|
Re: Problem with xmltextwriter Data at the root level is invalid. Line 1, position 1.
|
|
|
|
|
|
|
|
|
|
|
<Projectiles/> and <Projectiles></Projectiles> mean the same thing. If there are child nodes however, it has to be in the format <Projectiles> <ChildNodes/> </Projectile>
Hope it helps!
|
|
|
|
|
|