必要时继承TextReader
并覆盖ReadLine
替换每行字符的方法。将您的新流阅读器传递给XmlReader
,它将永远不会看到无效实体。
编辑:虽然XmlReader
支持TextReader
,但它似乎没有调用该ReadLine
方法......相反,看起来我们可能必须Stream
直接子类化并使用byte[] buffer
.
编辑:以下是我认为应该过滤通用流并用编码实体替换&符号的内容。由于它使用流字节,它没有任何编码概念,因此假定为 ASCII。它目前只处理&符号,但对其他实体进行编码也应该是微不足道的。它在一个 2.5 Gb 的文件上进行了尝试,发现它只增加了大约 3% 的处理时间,而且内存负载似乎没有受到影响。话虽如此,它肯定没有经过充分的测试。我敦促您确保它生成的文件与具有不同缓冲区大小的输入文件相同。例如:
using (var output = File.Open(@"out.xml", FileMode.Create))
using (var input = new AmpersandFilterText(File.Open(@"in.xml", FileMode.Open))
input.CopyTo(output);
然后对文件进行二进制比较。
编辑#3:我误读了 上的方法签名TextReader
,XmlReader
只是调用Read(char[], int, int)
. 无论如何,请参阅下面的不依赖于字符编码的实现。
class TestProgram
{
static void Main(string[] args)
{
var stringReader = new MemoryStream(Encoding.ASCII.GetBytes("<name>Me & You</name>"));
var textReader = new AmpersandFilterText(stringReader);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
using (XmlReader xreader = XmlReader.Create(textReader, settings))
{
while (xreader.Read())
{
Console.WriteLine(xreader.Value);
}
}
Console.ReadLine();
}
}
class AmpersandFilterText : StreamReader
{
public AmpersandFilterText(Stream stream)
: base(stream)
{
}
private StringBuilder sbBuffer = null;
private int bufferOffset = 0;
public override string ReadLine()
{
var rawLine = base.ReadLine();
rawLine = rawLine.Replace("&", "&");
return rawLine;
}
public override int Read()
{
char cChar;
if (sbBuffer != null)
{
cChar = sbBuffer[bufferOffset];
bufferOffset++;
evalBuffer();
return cChar;
}
else
{
cChar = (char)base.Read();
if (cChar == '&')
sbBuffer = new StringBuilder("amp;");
return cChar;
}
}
public override int Read(char[] buffer, int index, int count)
{
int destOffset = 0;
const string replacement = "&";
//exhaust our buffer first
if (sbBuffer != null)
{
int bufferedToConsume = Math.Min(count, sbBuffer.Length - bufferOffset);
sbBuffer.CopyTo(bufferOffset, buffer, index, bufferedToConsume);
destOffset += bufferedToConsume;
bufferOffset += bufferedToConsume;
evalBuffer();
}
//then, if needed, read more data
if (destOffset < count)
{
char[] newBuffer = new char[count - destOffset];
var newRead = base.Read(newBuffer, 0, newBuffer.Length);
//process new data and return directly
int sourceOffset = 0;
while (sourceOffset < newRead && destOffset < count)
{
char tChar = newBuffer[sourceOffset];
if (tChar == '&')
{
int replacementOffset = 0;
while (replacementOffset < replacement.Length && destOffset < count)
{
buffer[destOffset + index] = replacement[replacementOffset];
destOffset++;
replacementOffset++;
}
sourceOffset++;
//we did not copy the entire replacement
if (replacementOffset < replacement.Length)
{
//put the remainder in next time
sbBuffer = new StringBuilder();
sbBuffer.Append(replacement, replacementOffset, replacement.Length - replacementOffset);
}
}
else
{
buffer[destOffset + index] = tChar;
destOffset++;
sourceOffset++;
}
}
//we have unused data
if (sourceOffset < newRead)
{
if (sbBuffer == null)
sbBuffer = new StringBuilder();
//add data after replace
for (; sourceOffset < newRead; sourceOffset++)
{
char tChar = newBuffer[sourceOffset];
if (tChar == '&')
sbBuffer.Append(replacement);
else
sbBuffer.Append(tChar);
}
}
}
return destOffset;
}
public override int ReadBlock(char[] buffer, int index, int count)
{
return this.Read(buffer, index, count);
}
public override int Peek()
{
if (sbBuffer != null)
return sbBuffer[bufferOffset];
return base.Peek();
}
public override string ReadToEnd()
{
string cLine;
StringBuilder sbTemp = new StringBuilder();
while ((cLine = this.ReadLine()) != null)
sbTemp.AppendLine(cLine);
return sbTemp.ToString();
}
private void evalBuffer()
{
if (bufferOffset == sbBuffer.Length)
{
//TODO: check perf on sbBuffer.Clear
sbBuffer = null;
bufferOffset = 0;
}
}
}