Nevron Chart
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » Problem with xmltextwriter Data at the root level is invalid. Line 1, position 1.
       
Author Reply
Jason
posted 3 posts
since Jun 10, 2008 
from

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

  Posted on: 29 Jul 2010       

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!
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.
  Posted on: 29 Jul 2010        0  
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.
  Posted on: 29 Jul 2010        0  

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.
  Posted on: 29 Jul 2010   Accepted Answer     0  
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.
  Posted on: 29 Jul 2010        0  

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.
  Posted on: 29 Jul 2010        0  


<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!
       
6 Months Free & No Setup Fees ASP.NET Hosting!
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved