public void ReadingXmlWithStreamReaderCanThrowErrorsWhenReading()
        {
            string xml = "<?xml version='1.0' encoding='utf-16'?><hello><world/></hello>";

            using (StreamReader sr = new StreamReader(StreamUtils.UnicodeMemoryStreamWithBom(xml)))
            {
                //reading here causes exceptions reading xml later
                sr.ReadToEnd();
                sr.BaseStream.Position = 0;
                string output2 = sr.ReadToEnd();

                //not testing yet just clarifying assumptions
                Assert.That(output2.StartsWith(StreamUtils.BomFor(Encoding.Unicode)),
                            Is.True,
                            "Bom character output on the second read");

                sr.BaseStream.Position = 0;
                using (XmlTextReader xmlReader = new XmlTextReader(sr))
                {
                    try
                    {
                        xmlReader.Read();
                        Assert.Fail("Should have already failed");
                    }
                    catch (XmlException ex)
                    {
                        Assert.That(ex.Message, Text.StartsWith("Data at the root level is invalid"));
                    }
                }
            }
        }
Пример #2
0
        public void Gotcha_StreamReader_OnSecondReadWillWriteOutBomEmbeddedInStream()
        {
            string bom = StreamUtils.BomFor(Encoding.Unicode);

            using (StreamReader reader = new StreamReader(StreamUtils.UnicodeMemoryStreamWithBom("Hello World")))
            {
                Assert.That(reader.ReadToEnd().Contains(bom), Is.False);
                reader.BaseStream.Position = 0;
                Assert.That(reader.ReadToEnd().Contains(bom), Is.True);
            }
        }