28

I am reading xml from xxx URl but i am getting error as Root element is missing.

My code to read xml response is as follows:

  XmlDocument doc = new XmlDocument();
  doc.Load("URL from which i am reading xml");
  XmlNodeList nodes = doc.GetElementsByTagName("Product");
  XmlNode node = null;
  foreach (XmlNode n in nodes)
   {
   }

and the xml response is as follows:

<All_Products>
   <Product>
  <ProductCode>GFT</ProductCode>
  <ProductName>Gift Certificate</ProductName>
  <ProductDescriptionShort>Give the perfect gift. </ProductDescriptionShort>
  <ProductDescription>Give the perfect gift.</ProductDescription>
  <ProductNameShort>Gift Certificate</ProductNameShort> 
  <FreeShippingItem>Y</FreeShippingItem>
  <ProductPrice>55.0000</ProductPrice>
  <TaxableProduct>Y</TaxableProduct>
   </Product>    
 </All_Products>

Can you please tell where i am going wrong.

3
  • 1
    Could it be the fact that there's no XML declaration? Commented Apr 12, 2012 at 14:38
  • The above code with the xml you provided works (no errors) under LINQPad. Perhaps the xml you paste isn't quite what gets read? Are you certain doc.Load works and loads the content you provided? Commented Apr 12, 2012 at 14:39
  • 1
    I checked you code and its working fine at my end, are you sure you are not getting empty XML?
    – FIre Panda
    Commented Apr 12, 2012 at 14:44

7 Answers 7

100

Just in case anybody else lands here from Google, I was bitten by this error message when using XDocument.Load(Stream) method.

XDocument xDoc = XDocument.Load(xmlStream);  

Make sure the stream position is set to 0 (zero) before you try and load the Stream, its an easy mistake I always overlook!

if (xmlStream.Position > 0)
{
    xmlStream.Position = 0;
}
XDocument xDoc = XDocument.Load(xmlStream); 
2
  • 1
    Thanks, this helped. Mainly as I was getting correct deserialization after a serialization ,but on deserialization of an existing file: nada. Implying the position was being offset, or not set in the system backend. Bit of a bug from MS if you ask me, Commented Nov 24, 2019 at 21:31
  • You are a saviour!! Commented Jun 1 at 12:12
18

Make sure you XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<rootElement>
...
</rootElement>

Also, as noted at https://landesk378.rssing.com/chan-11533214/article34.html

a blank XML file will return the same Root elements is missing exception. Each XML file must have a root element / node which encloses all the other elements.

0
6

Hi this is odd way but try it once

  1. Read the file content into a string
  2. print the string and check whether you are getting proper XML or not
  3. you can use XMLDocument.LoadXML(xmlstring)

I try with your code and same XML without adding any XML declaration it works for me

XmlDocument doc = new XmlDocument();
        doc.Load(@"H:\WorkSpace\C#\TestDemos\TestDemos\XMLFile1.xml");
        XmlNodeList nodes = doc.GetElementsByTagName("Product");
        XmlNode node = null;
        foreach (XmlNode n in nodes)
        {
            Console.WriteLine("HI");
        }

As stated by Phil in below answer please set the xmlStream position to zero if it is not zero.

if (xmlStream.Position > 0)
{
    xmlStream.Position = 0;
}
XDocument xDoc = XDocument.Load(xmlStream); 
0
3

If you are loading the XML file from a remote location, I would check to see if the file is actually being downloaded correctly using a sniffer like Fiddler.

I wrote a quick console app to run your code and parse the file and it works fine for me.

2
  1. Check the trees.config file which located in config folder... sometimes (I don't know why) this file became to be empty like someone delete the content inside... keep backup up of this file in your local pc then when this error appear - replace the server file with your local file. This is what i do when this error happened.

  2. check the available space on the server. sometimes this is the problem.

Good luck.

1
  • Hello Idoshin, upvoted for point #2 because we saw this error at a customer site whereas the c: drive containing the ASP.NET ran out of space and a the contents of an XML settings file for that application got cleared out and broke the application with that error. Have you seen this happen and have you seen any "official" documentation that correlates this problem to lack of disk space? Commented May 4, 2021 at 15:02
2

I had the same problem when i have trying to read xml that was extracted from archive to memory stream.

 MemoryStream SubSetupStream = new MemoryStream();
        using (ZipFile archive = ZipFile.Read(zipPath))
        {
            archive.Password = "SomePass";
            foreach  (ZipEntry file in archive)
            {
                file.Extract(SubSetupStream);
            }
        }

Problem was in these lines:

XmlDocument doc = new XmlDocument();
    doc.Load(SubSetupStream);

And solution is (Thanks to @Phil):

        if (SubSetupStream.Position>0)
        {
            SubSetupStream.Position = 0;
        }
0

In my case, I found application settings to be corrupted. So to solve it, simply delete folder in local appdata%appdata%/../Local/YOUR_APP_NAME.

Not the answer you're looking for? Browse other questions tagged or ask your own question.