void WriteCheckedBuffer (char [] text, int idx, int length)
{
int start = idx;
int end = idx + length;
while ((idx = XmlChar.IndexOfInvalid (text, start, length, true)) >= 0) {
if (check_character_validity) // actually this is one time pass.
throw ArgumentError (String.Format ("Input contains invalid character at {0} : &#x{1:X};", idx, (int) text [idx]));
if (start < idx)
writer.Write (text, start, idx - start);
writer.Write ("&#x");
writer.Write (((int) text [idx]).ToString (
"X",
CultureInfo.InvariantCulture));
writer.Write (';');
length -= idx - start + 1;
start = idx + 1;
}
if (start < end)
writer.Write (text, start, end - start);
}