internal int AppendEscapeXmlString(string s, bool inAttribute, char quoteChar) {
int charactersWritten = 0;
for (int i = 0; i < s.Length; i++) {
char ch = s[i];
bool appendCharEntity = false;
string entityRef = null;
if ((ch < 32 && ch != '\t' && ch != '\r' && ch != '\n') || (ch > 0xFFFD)) {
appendCharEntity = true;
}
else {
switch (ch)
{
case '<':
entityRef = "lt";
break;
case '>':
entityRef = "gt";
break;
case '&':
entityRef = "amp";
break;
case '\'':
if (inAttribute && quoteChar == ch) {
entityRef = "apos";
}
break;
case '"':
if (inAttribute && quoteChar == ch) {
entityRef = "quot";
}
break;
case '\n':
case '\r':
appendCharEntity = inAttribute;
break;
default:
break;
}
}
if (appendCharEntity) {
charactersWritten += AppendCharEntity(ch);
}
else if (entityRef != null) {
charactersWritten += AppendEntityRef(entityRef);
}
else {
charactersWritten += Write(ch);
}
}
return charactersWritten;
}