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:

MemoryStream xmlStream = new MemoryStream();
            StreamWriter xmlStreamWriter = new StreamWriter(xmlStream);
            String xml = "<html>\n<body>\n\n<h1>My First Heading</h1>\n\n<p>My first paragraph.</p>\n\n</body>\n</html>";
            xmlStreamWriter.Write(xml);
            xmlStreamWriter.Flush();
            XmlReader reader = XmlReader.Create(xmlStream);

            StringWriter jsonStringWriter = new StringWriter();
            JsonWriter writer = new JsonTextWriter(jsonStringWriter);
            writer.Formatting = Newtonsoft.Json.Formatting.Indented;
            JsonMLParser.Encode(reader, writer);

The exception gets thrown during JsonMLParser.Encode() during reader.MoveToContent() which is the first call to the xml-reader.

Edited 7 Years Ago by Alxandr: n/a

Reset the stream position before instantiate the reader.

xmlStream.Position = 0;
XmlReader reader = XmlReader.Create(xmlStream);
reader.MoveToContent();
.....
Comments
good catch :p

Perhaps because you're trying to parse html/xhtml with an xml reader, and they're different formats.

Reset the stream position before instantiate the reader.

xmlStream.Position = 0;
XmlReader reader = XmlReader.Create(xmlStream);
reader.MoveToContent();
.....

Thnx. You're completely right.

This question has already been answered. Start a new discussion instead.