944,162 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 4240
  • C# RSS
Nov 4th, 2009
0

Strange Root element missing exception in XML

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  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; Nov 4th, 2009 at 6:29 pm.
Similar Threads
Reputation Points: 15
Solved Threads: 10
Junior Poster in Training
Alxandr is offline Offline
73 posts
since May 2009
Nov 4th, 2009
3
Re: Strange Root element missing exception in XML
Reset the stream position before instantiate the reader.
C# Syntax (Toggle Plain Text)
  1. xmlStream.Position = 0;
  2. XmlReader reader = XmlReader.Create(xmlStream);
  3. reader.MoveToContent();
  4. .....
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Nov 5th, 2009
0
Re: Strange Root element missing exception in XML
Perhaps because you're trying to parse html/xhtml with an xml reader, and they're different formats.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 5th, 2009
0
Re: Strange Root element missing exception in XML
Click to Expand / Collapse  Quote originally posted by adatapost ...
Reset the stream position before instantiate the reader.
C# Syntax (Toggle Plain Text)
  1. xmlStream.Position = 0;
  2. XmlReader reader = XmlReader.Create(xmlStream);
  3. reader.MoveToContent();
  4. .....
Thnx. You're completely right.
Reputation Points: 15
Solved Threads: 10
Junior Poster in Training
Alxandr is offline Offline
73 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: C# Serial Ports
Next Thread in C# Forum Timeline: How to display values from database into datagridview





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC