none
Strange XML issue with "There is an unclosed literal string"

    Question

  • I have the XML output of a DataSet.WriteXml (with XmlWriteMode.WriteSchema) in a file. Parsing this file works fine if I open a stream to it using File.OpenRead() and passing it to XmlDocument.Load().

    However, if I try creating the stream as described below, it fails (this is just a reduced example, in real life it's WebClient response I'm parsing, but the error is the same).

    MemoryStream ms = new MemoryStream();
    StreamWriter sw = new StreamWriter(ms);
    sw.Write(
    File.ReadAllText("File.txt"));
    ms.Position = 0;


    I get "There is an unclosed literal string" errors - if I remove the "faulty" line, the error will just occur earlier in the stream (or, when removing enough tags, an "root element is missing").
    I've tried using an XmlReader to parse it step by step, and it parses tags properly until the error occurs. I find it strange that removing that element would create an error *earlier* in the file... Closing tags are just fine.

    So, the data isn't malformed, as shown by using the File.OpenRead() stream. Why does it occur with the second approach?
    Note that I've tried different encodings too, with no success.

    Thanks.

    Sunday, March 01, 2009 7:29 PM

Answers

  • As always, the answer appears right after posting... Of course, the StreamWriter needs to be Flush()'ed!
    Sunday, March 01, 2009 7:39 PM