In certain conditions that I cannot predict the following expression that uses a XmlTextWriter
writer.WriteValue(value)
throws an ArgumentException
because the data does contain invalid XML characters.
When the Exception is thrown I can fix the XML by sanitizing the string like this
var validXmlChars = text.Where(XmlConvert.IsXmlChar).ToArray();
return new string(validXmlChars);
The problem is that when I try to do that and perform the write operation again, I get an InvalidOperationException that says that
The Content token in Error state will cause the XML to be invalid
Is there a way to reset the XmlTextWriter and write the value again with WriteValue so I can write the proper string after an exception has occurred?
Thank you.