0

I'm trying to parse xml from a website, and I'm getting the error "Data at the root level is invalid. Line 1, position 1.

Here is the website output:

<Forecast xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<Days>

<Day>

<date>16/06/2016</date>

...etc

I don't know why the first line is causing an error, but is it possible to skip that line when I'm reading, and just get to the Days element? I've read other questions on here that have this same error but none of the solutions worked for me.

I'm trying to read the xml this way:

XmlDocument doc1 = new XmlDocument();
doc1.Load(url);
XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/Forecast/Days/Day");
foreach (XmlNode node in nodes)
{
            string max = node["temp_max_f"].InnerText;
            string min = node["temp_min_f"].InnerText;
            string date = node["date"].InnerText;
            Console.WriteLine("{0}   {1}    {2}", date, max, min);

} 
issharp
  • 310
  • 6
  • 22
  • Is that a public URL, can you share it so that we can check? Usually an XML document does not start with such a text, rather it is the browser that adds that line when rendering the XML. What does "View source" in the browser show? – Martin Honnen Jun 17 '16 at 13:08
  • I'd rather not post the url because it contains my private API key. You're right, though--when I view the source, the first line isn't there. Sorry about that. It then shows the rest like I've given, except without the line breaks, etc. – issharp Jun 17 '16 at 13:26

1 Answers1

2

The first line you provide is normally generated by the browser while trying to find style information. Edit: You changed that now. So not to confuse other people I crossed it out.

So in fact you are looking at the plain XML when downloading the file. My first intention is the BOM (also found here and solution should be here).

While trying to parse the XML data the parser instead finds the Byte Order Mark (BOM) and throws an error. So removing the BOM like in the example should fix the problem.

Community
  • 1
  • 1
  • The example you've linked to removes the BOM from the xml string. I'm getting the error when I try to load the data, so I don't have a string to work with. Do you know how to deal with this case? – issharp Jun 17 '16 at 13:33
  • Is downloading the file to a string and then parsing it from there an option? There are several sources which say UTF-8 and Byte Order Marks shouldn't be a problem because the load function finds them by itself. Can't really test it now. I could have a deeper look on this later. – Markus Laubenthal Jun 17 '16 at 14:48
  • I'm not sure. Eventually, this is going to be an automated call that happens several times a day, so I don't think at that point downloading it will be possible, but maybe it's worth exploring until I figure out the problem? – issharp Jun 17 '16 at 15:29
  • I meant that you could go for this approach. So you are downloading the file before you parse it. And thus you are able to manipulate the data as needed: using (var wc = new System.Net.WebClient()) contents = wc.DownloadString(url); – Markus Laubenthal Jun 17 '16 at 16:45
  • Okay, I tried that approach, and now I'm getting the error "There is an error in XML document (1,1)" when I try to parse the document (I've already removed the BOM the way they said to). – issharp Jun 20 '16 at 12:40