Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / November 2003

Tip: Looking for answers? Try searching our database.

System.Xml.XmlException:  hexadecimal value is an invalid character

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Todd - 27 Nov 2003 16:41 GMT
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:
&lt;?xml version='1.0' encoding='UTF-8'?
&gt;&lt;ReportXML&gt;&lt;/ReportXML&gt;
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.