Sanitizing data is important. Sometimes edge cases - invalid characters in "XML" - do occur. Your solution is correct. If you want a solution that fits into the .NET framework in regards to streaming restructure your code to fit into its own Stream:
public class SanitizedStreamReader : StreamReader {
public SanitizedStreamReader(string filename) : base(filename) { }
/* other ctors as needed */
// it is unclear from the documentation which methods call each other
// so best bet is to override all of the Read* methods and Peak
public override string ReadLine() {
return Sanitize(base.ReadLine());
}
// TODO override Read*, Peak with a similar logic as this.ReadLine()
// remember Read(Char[], Int32, Int32) to modify the return value by
// the number of removed characters
private static string Sanitize(string unclean) {
if (String.IsNullOrEmpty(unclean)
return "";
return unclean.Replace(((char)4).ToString(), "").Replace(((char)0x14);
}
}
With this new SanitizedStreamReader
you'll be able to chain it into processing streams as necessary, rather than relying on a magic method to clean things and present you with an XmlTextReader:
return new XmlTextReader(new SanitizedStreamReader("filename.xml"));
Admittedly this may be more work than necessary but you will gain flexibility from this approach.