Strange Root element missing exception in XML

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 71
Reputation: Alxandr is an unknown quantity at this point 
Solved Threads: 9
Alxandr's Avatar
Alxandr Alxandr is offline Offline
Junior Poster in Training

Strange Root element missing exception in XML

 
0
  #1
23 Days Ago
I get an error trying to parse som XML. The error is System.Xml.XmlException: Root element is missing.. , and I can't understand why. Here is the code I run:
  1. MemoryStream xmlStream = new MemoryStream();
  2. StreamWriter xmlStreamWriter = new StreamWriter(xmlStream);
  3. String xml = "<html>\n<body>\n\n<h1>My First Heading</h1>\n\n<p>My first paragraph.</p>\n\n</body>\n</html>";
  4. xmlStreamWriter.Write(xml);
  5. xmlStreamWriter.Flush();
  6. XmlReader reader = XmlReader.Create(xmlStream);
  7.  
  8. StringWriter jsonStringWriter = new StringWriter();
  9. JsonWriter writer = new JsonTextWriter(jsonStringWriter);
  10. writer.Formatting = Newtonsoft.Json.Formatting.Indented;
  11. JsonMLParser.Encode(reader, writer);

The exception gets thrown during JsonMLParser.Encode() during reader.MoveToContent() which is the first call to the xml-reader.
Last edited by Alxandr; 23 Days Ago at 6:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven
 
3
  #2
23 Days Ago
Reset the stream position before instantiate the reader.
  1. xmlStream.Position = 0;
  2. XmlReader reader = XmlReader.Create(xmlStream);
  3. reader.MoveToContent();
  4. .....
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,200
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #3
22 Days Ago
Perhaps because you're trying to parse html/xhtml with an xml reader, and they're different formats.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 71
Reputation: Alxandr is an unknown quantity at this point 
Solved Threads: 9
Alxandr's Avatar
Alxandr Alxandr is offline Offline
Junior Poster in Training
 
0
  #4
22 Days Ago
Originally Posted by adatapost View Post
Reset the stream position before instantiate the reader.
  1. xmlStream.Position = 0;
  2. XmlReader reader = XmlReader.Create(xmlStream);
  3. reader.MoveToContent();
  4. .....
Thnx. You're completely right.
Reply With Quote Quick reply to this message  
Reply

Tags
xml

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC