Our ASP.NET (C#) application accepts form entry and saves
inputed data in XML.
We are finding that users are sometimes cutting and
pasting special characters (from MS Word) into these
forms. The data is saved successfuly, but when the XML is
later read the following error is encountered depending on
the invalid character found:
This is a sample:
System.Xml.XmlException: '', hexadecimal value 0x1A, is
an invalid character.
I have ensured that the saved XML includes an encoding
declaration (<?xml version="1.0" encoding ='UTF-8'?>).
Changing the encoding format does not effect the error
message.
The XML parser installed is MSXML 4.0.
Is there any way to handle the reading of these
characters, or any way to ensure these characters are
converted into something readable at the time the values
are written to the XML object?
Any help is greatly appreciated.
Oleg Tkachenko - 27 Nov 2003 16:55 GMT
> Our ASP.NET (C#) application accepts form entry and saves
> inputed data in XML.
[quoted text clipped - 4 lines]
> later read the following error is encountered depending on
> the invalid character found:
How do you save that data? Show us how do you create that XML.
It could be the problem with XmlTextWriter, which doesn't check unicode
characters that do not fit the specified encoding and hence can produce
non well-formed XML. See "Customized XML Writer Creation" article [1] in
msdn how to handle the issue.
[1]
ms-help://MS.MSDNQTR.2003JUL.1033/cpguide/html/cpconwritingxmlwithxmlwriter.htm

Signature
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Todd - 27 Nov 2003 17:12 GMT
>-----Original Message-----
>
[quoted text clipped - 15 lines]
>[1]
>ms-help://MS.MSDNQTR.2003JUL.1033/cpguide/html/cpconwritingxml
withxmlwriter.htm
This is how the XML is created:
protected void Application_Start(Object sender, EventArgs
e)
{
XmlDocument xml = new XmlDocument
();
StringBuilder sb = new
StringBuilder();
StringWriter sw = new StringWriter
(sb);
XmlTextWriter xmlText = new
XmlTextWriter(sw);
xml.LoadXml
(ConstantsXML.getInstance().GetValueFromKey
("ReportXmlTemplate"));
xml.WriteContentTo(xmlText);
Application["ReportXml"] =
sb.ToString();
}
Where the value for "ReportXmlTemplate" is:
<?xml version='1.0' encoding='UTF-8'?
><ReportXML></ReportXML>
Dave Marteinson - 28 Nov 2003 19:35 GMT
Hi Todd,
>This is a sample:
>System.Xml.XmlException: '', hexadecimal value 0x1A, is
>an invalid character.
I'm not aware of anything that'll let you pick out these characters
from an instance and correct the problem. Always a tricky issue.
It's not clear to me that DOM Level 3 Validation will handle
this either, although I've just skimmed that.
(http://www.w3.org/TR/2003/CR-DOM-Level-3-Val-20030730/)
Your case is simpler since you say the encoding is under
your control. There aren't too many ranges that aren't allowed,
so perhaps you can just filter these out upstream.
From 2.2 of the rec "Characters":
[2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
[#x10000-#x10FFFF]
In particular (re your sample error), if you see
anything < #x20 that's not a tab, lf or cr convert it or remove it.
Regards,
-djm