We collect many lines and send them to our clients in xml fragments. These lines can contain literally any character. We found an error caused by an attempt to serialize XElement instances containing bad characters. Here is an example:
var message = new XElement("song"); char c = (char)0x1a;
The above code throws an exception on the specified line. Here's the stacktrace:
'SUB', hexadecimal value 0x1A, is an invalid character. System.ArgumentException System.ArgumentException: '', hexadecimal value 0x1A, is an invalid character.
at System.Xml.XmlEncodedRawTextWriter.InvalidXmlChar (Int32 ch, Char * pDst, Boolean entitize)
at System.Xml.XmlEncodedRawTextWriter.WriteAttributeTextBlock (Char * pSrc, Char * pSrcEnd)
at System.Xml.XmlEncodedRawTextWriter.WriteString (String text)
at System.Xml.XmlWellFormedWriter.WriteString (String text)
at System.Xml.XmlWriter.WriteAttributeString (String prefix, String localName, String ns, String value)
at System.Xml.Linq.ElementWriter.WriteStartElement (XElement e)
at System.Xml.Linq.ElementWriter.WriteElement (XElement e)
at System.Xml.Linq.XElement.WriteTo (XmlWriter writer)
at System.Xml.Linq.XNode.GetXmlString (SaveOptions o)
My suspicion is that this is incorrect behavior, and a bad char should be escaped in XML. Whether this is desirable or not is a question that I will answer later.
So here is the question:
Is there a way to handle strings in such a way that this error may not occur, or should I just delete all characters below char 0x20 and cross my fingers?
spender
source share