Now I have implemented a TextReader wrapper that introduces NewLine. I include it here if someone finds it useful, and also if someone has a more elegant solution - if so, please comment!
It uses the XmlReader only to call the Read (...) method to read data - otherwise it throws a NotImplementedException.
In the above example, you will use it as follows:
textReader = new NewlineAfterXmlDeclReader(new StringReader(fails));
This is an implementation
class NewlineAfterXmlDeclReader : TextReader { private const int InitialChunkSize = 80; private const string SearchText = "?><!" + "DOCTYPE"; //concatenation injected for readability in SO purposes only private static readonly string ReplaceText = "?>" + Environment.NewLine + "<!" + "DOCTYPE"; private readonly TextReader _wrappedReader; private TextReader _firstChunkReader; public NewlineAfterXmlDeclReader(TextReader wrappedReader) { _wrappedReader = wrappedReader; var initialChunk = new char[InitialChunkSize]; var count = _wrappedReader.Read(initialChunk, 0, InitialChunkSize); var initialChunkString = new String(initialChunk, 0, count); _firstChunkReader = new StringReader(initialChunkString.Replace(SearchText, ReplaceText)); } public override int Read(char[] buffer, int index, int count) { var firstChunkReadCount = 0; if (_firstChunkReader != null) { firstChunkReadCount = _firstChunkReader.ReadBlock(buffer, index, count); if (firstChunkReadCount == count) return firstChunkReadCount; _firstChunkReader = null; index += firstChunkReadCount; count -= firstChunkReadCount; } return firstChunkReadCount + _wrappedReader.Read(buffer, index, count); } public override void Close() { _wrappedReader.Close(); } protected override void Dispose(bool disposing) { _wrappedReader.Dispose(); } public override int Peek() { throw new NotImplementedException(); } public override int Read() { throw new NotImplementedException(); } public override string ReadToEnd() { throw new NotImplementedException(); } public override int ReadBlock(char[] buffer, int index, int count) { throw new NotImplementedException(); } public override string ReadLine() { throw new NotImplementedException(); } }
Damian
source share