FIX: System.ArgumentException: ”, hexadecimal value 0x07, is an invalid character.

If you get the following exception, try specifying CheckCharacters as false on XmlWriterSettings:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.CheckCharacters = false;

XmlWriter writer = XmlTextWriter.Create(sw, settings);

System.ArgumentException: ”, hexadecimal value 0x07, is an invalid character.
   at System.Xml.XmlEncodedRawTextWriter.InvalidXmlChar(Int32 ch, Char* pDst, Boolean entitize)
   at System.Xml.XmlEncodedRawTextWriter.WriteElementTextBlock(Char* pSrc, Char* pSrcEnd)
   at System.Xml.XmlEncodedRawTextWriter.WriteString(String text)
   at System.Xml.XmlEncodedRawTextWriterIndent.WriteString(String text)
   at System.Xml.XmlRawWriter.WriteValue(String value)
   at System.Xml.XmlWellFormedWriter.WriteValue(String value)
   at System.Runtime.Serialization.StringDataContract.WriteXmlValue(XmlWriterDelegator writer, Object obj, XmlObjectSerializerWriteContext context)


I had to do this because I didn’t care that 0x07 was an invalid character and the exception only started happening when I used XmlWriterSettings for indentation.

This is an alternative to Robert Horvick’s now dated article on MSDN.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s