Home > ASP.NET > XML Parsing Error: reference to invalid character number. Hexadecimal value 0x[whatever] is an invalid character

XML Parsing Error: reference to invalid character number. Hexadecimal value 0x[whatever] is an invalid character

If you get an error parsing xml

“XML Parsing Error: reference to invalid character number

<Text> Some Text &#xB;</Text> ”

try to remove the unsupported invalid characters from the xml string as

writer.WriteStartElement(“Text”);

string text = sometext to be in xml;
StringBuilder buf = new StringBuilder(text.Length);
foreach (char ch in text)
{
if (IsLegalXmlCharacter(ch))
{
buf.Append(ch);
}
}
writer.WriteString(Convert.ToString(buf));
writer.WriteEndElement();

public bool IsLegalXmlCharacter(int ch)
{
return
(
ch == 0x9 || ch == 0xA || ch == 0xD ||  (ch >= 0x20    && ch <= 0xD7FF  ) ||
(ch >= 0x10000 && ch <= 0x10FFFF) || (ch >= 0xE000  && ch <= 0xFFFD  )

);
}

🙂

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: