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.

Reset the stream position before instantiate the reader.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.