C# – Root element is missing


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:

  <ProductName>Gift Certificate</ProductName>
  <ProductDescriptionShort>Give the perfect gift. </ProductDescriptionShort>
  <ProductDescription>Give the perfect gift.</ProductDescription>
  <ProductNameShort>Gift Certificate</ProductNameShort> 

Can you please tell where i am going wrong.

Best Solution

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);