Wednesday, February 6, 2013

How to fix "Hexadecimal value 0x is an invalid character"?

Because xml includes invalid character.
Add an filter following for xml content

/// 
/// Remove illegal XML characters from a string.
/// 
public string SanitizeXmlString(string xml)
{
 if (xml == null)
 {
  throw new ArgumentNullException("xml");
 }
 
 StringBuilder buffer = new StringBuilder(xml.Length);
 
 foreach (char c in xml)
 {
  if (IsLegalXmlChar(c))
  {
   buffer.Append(c);
  }
 }
  
 return buffer.ToString();
}

/// 
/// Whether a given character is allowed by XML 1.0.
/// 
public bool IsLegalXmlChar(int character)
{
 return
 (
   character == 0x9 /* == '\t' == 9   */          ||
   character == 0xA /* == '\n' == 10  */          ||
   character == 0xD /* == '\r' == 13  */          ||
  (character >= 0x20    && character <= 0xD7FF  ) ||
  (character >= 0xE000  && character <= 0xFFFD  ) ||
  (character >= 0x10000 && character <= 0x10FFFF)
 );
}



http://seattlesoftware.wordpress.com/2008/09/11/hexadecimal-value-0-is-an-invalid-character/

No comments:

Post a Comment