xml.LoadData - Data at the root level is invalid. Line 1, position 1
The hidden character is probably BOM.
The explanation to the problem and the solution can be found here, credits to James Schubert, based on an answer by James Brankin found here.
Though the previous answer does remove the hidden character, it also removes the whole first line. The more precise version would be:
string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (xml.StartsWith(_byteOrderMarkUtf8))
{
xml = xml.Remove(0, _byteOrderMarkUtf8.Length);
}
I encountered this problem when fetching an XSLT file from Azure blob and loading it into an XslCompiledTransform object.
On my machine the file looked just fine, but after uploading it as a blob and fetching it back, the BOM character was added.
Data at the root level is invalid. Line 1, position 1 -why do I get this error while loading an xml file?
The LoadXml
method is for loading an XML string directly. You want to use the Load
method instead.
Data at the root level is invalid
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.
XDocument.Load(url) Error: Data at the root level is invalid. Line 1 position 1
All I had to do was to set the headers to accept xml
like so:
try
{
string url = "http://myfirsturl.com";
var request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "application/xml"; // <== THIS FIXED IT
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
XDocument doc = XDocument.Load(stream);
Console.WriteLine(doc);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Thanks for the comments and help!
LoadXml - Data at the root level is invalid. Line 1, position 1
As you know that there is only one <ID>
, <birthday>
, etc. in each <student>
node you can use SelectSingleNode
.
Although it looks like .Value
would get you what appears to be the value, it is more fiddly than that: XmlNode Value vs InnerText.
I assume you put in the xmlns="namespace"
from seeing similar things in other XML. In this case, unless you are actually using it, it will only complicate matters.
To validate a date, use DateTime.TryParseExact and give it a format string - there is no need for a complicated regex which would need to be different if you changed to a better date format like yyyy-MM-dd.
You might as well declare the regexes outside the loop to keep the code inside the loop a bit tidier.
It is always frustrating to get a message that says something like "Date error" when it doesn't tell you where or what the erroneous data is.
So, with this XML file located in my "C:\Temp" directory (I don't know why you would use "us-ascii" rather than "utf-8"):
<?xml version="1.0" encoding="us-ascii" standalone="yes"?>
<studentTable>
<student>
<ID>0q</ID>
<student_name>John*</student_name>
<birthday>25/109/1997</birthday>
</student>
</studentTable>
and this console application:
Imports System.Text.RegularExpressions
Imports System.Xml
Module Module1
Sub Main()
Dim lstErrs As New List(Of String)
Dim idRegex = New Regex("^[0-9]*$")
Dim nameRegex = New Regex("^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$")
Dim dateFormat = "d/M/yyyy"
Dim xdoc As New XmlDocument()
xdoc.Load("C:\Temp\students.xml")
Dim nodelist = xdoc.SelectNodes("//studentTable/student")
For Each node As XmlNode In nodelist
Dim id = node.SelectSingleNode("//ID").InnerText
Dim dob = node.SelectSingleNode("//birthday").InnerText
Dim name = node.SelectSingleNode("//student_name").InnerText
If Not idRegex.IsMatch(id) Then
lstErrs.Add("Invalid ID number " & id)
End If
If Not DateTime.TryParseExact(dob, dateFormat, Nothing, Nothing, New DateTime) Then
lstErrs.Add("Invalid birthday " & dob)
End If
If Not nameRegex.IsMatch(name) Then
lstErrs.Add("Invalid Name " & name)
End If
Console.WriteLine($"{id} {dob} {name}") '' for checking
Next
Console.WriteLine(String.Join(vbCrLf, lstErrs)) '' show the errors
Console.ReadLine()
End Sub
End Module
I got this output:
0q 25/109/1997 John*
Invalid ID number 0q
Invalid birthday 25/109/1997
Invalid Name John*
Related Topics
String = String + Int: What's Behind the Scenes
Format an Excel Column (Or Cell) as Text in C#
Getting the Thread Id from a Thread
Hyphenated HTML Attributes with ASP.NET MVC
Converting Bitmap Pixelformats in C#
Convert a Binary String Representation to a Byte Array
Finding an Enum Value by Its Description Attribute
Entity Framework 4 Delete Object from Entity Collection
Nhibernate Aliastobean Transformer Associations
How to Set Properties on Struct Instances Using Reflection
Why Is Cross Thread Operation Exception Not Thrown While Running Exe in Bin\Debug
Deserialize a JSON Array in C#
Console Application Closes Immediately After Opening in Visual Studio
Executing Ssis 2012 Package That Has Script Components from External Application
How Does JSON Deserialization in C# Work