63

I have the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<Offices id="0" enabled="false">
  <office />
</Offices>

When I try to access it through C#:

XmlDocument doc = new XmlDocument();
doc.LoadXml(HttpContext.Current.Server.MapPath("officeList.xml"));

I get this error:

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

What is wrong with this line?

perrr
  • 5
  • 1
  • 2
phil crowe
  • 633
  • 1
  • 5
  • 4
  • Note: although this question is older than the question I just closed it as a duplicate of, the later question is more to the point. In particular, it doesn't cloud the issue by using `HttpContext`. – John Saunders Jul 23 '14 at 02:39
  • 2
    I just had this error on a resx file in Visual Studio and it was resolved by closing Visual Studio and opening it again. – jag Nov 28 '17 at 11:38
  • Check the web.config, I had an invalid character at the start of it. – niico Feb 19 '20 at 09:49
  • The dupe direction should be reversed on this. This one is older and has more answers covering more cases, in addition to the correct answer which the other one also has. – TylerH Dec 15 '21 at 16:55

3 Answers3

111

This:

doc.LoadXml(HttpContext.Current.Server.MapPath("officeList.xml"));

should be:

doc.Load(HttpContext.Current.Server.MapPath("officeList.xml"));

LoadXml() is for loading an XML string, not a file name.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • The last line helped me, I was facing a data root error but had no idea as was loading the configuration file from a project so I used xmlDoc.Load(filePath) and it worked!!! Thanks! – Anup Shah Sep 16 '21 at 11:33
  • XmlDocument.LoadXml() loads an XML string XmlDocument.Load() loads XML file – Anup Shah Sep 16 '21 at 11:58
25

For the record:

"Data at the root level is invalid" means that you have attempted to parse something that is not an XML document. It doesn't even start to look like an XML document. It usually means just what you found: you're parsing something like the string "C:\inetpub\wwwroot\mysite\officelist.xml".

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • ... or something that starts with an xml document specification line ... ?> as usual that definitely looks like an xml document to us, but not for XmlDocument.LoadXml(), see the comment by Joseph Morgan – Roland Aug 21 '14 at 16:53
1

I found that the example I was using had an xml document specification on the first line. I was using a stylesheet I got at this blog entry and the first line was

<?xmlversion="1.0"encoding="utf-8"?>

which was causing the error. When I removed that line, so that the stylesheet started with the line

<xsl:stylesheet version="1.0" xmlns:DTS="www.microsoft.com/SqlServer/Dts" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

my transform worked. By the way, that blog post was the first good, easy-to follow example I have found for trying to get information from the XML definition of an SSIS package, but I did have to modify the paths in the example for my SSIS 2008 packages, so you might too. I also created a version to extract the "flow" from the precedence constraints. My final one looks like this:

    <xsl:stylesheet version="1.0" xmlns:DTS="www.microsoft.com/SqlServer/Dts" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="utf-8" />
    <xsl:template match="/">
    <xsl:text>From,To~</xsl:text>
    <xsl:text>
</xsl:text>
    <xsl:for-each select="//DTS:PrecedenceConstraints/DTS:PrecedenceConstraint">
      <xsl:value-of select="@DTS:From"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="@DTS:To"/>
       <xsl:text>~</xsl:text>
      <xsl:text>
</xsl:text>
    </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

and gave me a CSV with the tilde as my line delimiter. I replaced that with a line feed in my text editor then imported into excel to get a with look at the data flow in the package.

Joseph Morgan
  • 163
  • 1
  • 9
  • 3
    This doesn't actually answer the question. – John Saunders Jul 23 '14 at 02:37
  • +1 for mentioning the problem of the doc spec line for XmlDocument.LoadXml(). My problem had the same exception of the original question, with the same LoadXml() call, and is now solved. – Roland Aug 21 '14 at 16:50