It's fairly straightforward to subclass TextReader
to read from an array of chars or equivalent. Here's a version that takes a ReadOnlyMemory<char>
that could represent a slice of either a string
or a char []
character array:
public sealed class CharMemoryReader : TextReader
{
private ReadOnlyMemory<char> chars;
private int position;
public CharMemoryReader(ReadOnlyMemory<char> chars)
{
this.chars = chars;
this.position = 0;
}
void CheckClosed()
{
if (position < 0)
throw new ObjectDisposedException(null, string.Format("{0} is closed.", ToString()));
}
public override void Close() => Dispose(true);
protected override void Dispose(bool disposing)
{
chars = ReadOnlyMemory<char>.Empty;
position = -1;
base.Dispose(disposing);
}
public override int Peek()
{
CheckClosed();
return position >= chars.Length ? -1 : chars.Span[position];
}
public override int Read()
{
CheckClosed();
return position >= chars.Length ? -1 : chars.Span[position++];
}
public override int Read(char[] buffer, int index, int count)
{
CheckClosed();
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index));
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count));
if (buffer.Length - index < count)
throw new ArgumentException("buffer.Length - index < count");
return Read(buffer.AsSpan().Slice(index, count));
}
public override int Read(Span<char> buffer)
{
CheckClosed();
var nRead = chars.Length - position;
if (nRead > 0)
{
if (nRead > buffer.Length)
nRead = buffer.Length;
chars.Span.Slice(position, nRead).CopyTo(buffer);
position += nRead;
}
return nRead;
}
public override string ReadToEnd()
{
CheckClosed();
var s = position == 0 ? chars.ToString() : chars.Slice(position, chars.Length - position).ToString();
position = chars.Length;
return s;
}
public override string ReadLine()
{
CheckClosed();
var span = chars.Span;
var i = position;
for( ; i < span.Length; i++)
{
var ch = span[i];
if (ch == '\r' || ch == '\n')
{
var result = span.Slice(position, i - position).ToString();
position = i + 1;
if (ch == '\r' && position < span.Length && span[position] == '\n')
position++;
return result;
}
}
if (i > position)
{
var result = span.Slice(position, i - position).ToString();
position = i;
return result;
}
return null;
}
public override int ReadBlock(char[] buffer, int index, int count) => Read(buffer, index, count);
public override int ReadBlock(Span<char> buffer) => Read(buffer);
public override Task<String> ReadLineAsync() => Task.FromResult(ReadLine());
public override Task<String> ReadToEndAsync() => Task.FromResult(ReadToEnd());
public override Task<int> ReadBlockAsync(char[] buffer, int index, int count) => Task.FromResult(ReadBlock(buffer, index, count));
public override Task<int> ReadAsync(char[] buffer, int index, int count) => Task.FromResult(Read(buffer, index, count));
public override ValueTask<int> ReadBlockAsync(Memory<char> buffer, CancellationToken cancellationToken = default) =>
cancellationToken.IsCancellationRequested ? new ValueTask<int>(Task.FromCanceled<int>(cancellationToken)) : new ValueTask<int>(ReadBlock(buffer.Span));
public override ValueTask<int> ReadAsync(Memory<char> buffer, CancellationToken cancellationToken = default) =>
cancellationToken.IsCancellationRequested ? new ValueTask<int>(Task.FromCanceled<int>(cancellationToken)) : new ValueTask<int>(Read(buffer.Span));
}
Then use it with one of the following extension methods:
public static partial class XmlSerializationHelper
{
public static T LoadFromXml<T>(this char [] xml, int contentLength, XmlSerializer serial = null) =>
new ReadOnlyMemory<char>(xml, 0, contentLength).LoadFromXml<T>(serial);
public static T LoadFromXml<T>(this ReadOnlyMemory<char> xml, XmlSerializer serial = null)
{
serial = serial ?? new XmlSerializer(typeof(T));
using (var reader = new CharMemoryReader(xml))
return (T)serial.Deserialize(reader);
}
}
E.g.
var result = buffer.LoadFromXml<MyEntity>(contentLength, _xmlSerializer);
Notes:
A char []
character array has basically the same contents as a UTF-16 encoded memory stream without a BOM, so one could create a custom Stream
implementation resembling MemoryStream
that represents each char
as two bytes, as is done in this answer to How do I generate a stream from a string? by György Kőszeg. It looks a bit tricky to do this entirely correctly however, as getting all the async
methods right seems nontrivial.
Having done so XmlReader
will still need to wrap the custom stream with a StreamReader
that "decodes" the stream into a sequence of characters, correctly inferring the encoding in the process (which I have observed may occasionally be done wrongly, e.g. when the encoding stated the XML declaration does not match the actual encoding).
I chose to create a custom TextReader
rather than a custom Stream
to avoid the unnecessary decoding step, and because the async
implementation seemed less burdensome.
Representing each char
as a single byte via truncation (e.g. (byte)str[i]
) will corrupt XML containing any multibyte characters.
I haven't done any performance tuning on the above implementation.
Demo fiddle here.