Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
264 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...