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 / June 2007

Tip: Looking for answers? Try searching our database.

Unable to translate Unicode character \u00E9 at index 5409 to specified code page.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Les Caudle - 20 Jun 2007 15:41 GMT
I've got some C# 2.0 code that has been working for a year.

using (XmlWriter w = XmlWriter.Create("out.xml" ,settings)) {
    // many lines of code to write to w
    w.WriteStartElement("contactTypeRef");
   
Suddently, I'm getting this 100% repeatable error:

************** Exception Text **************
System.Text.EncoderFallbackException: Unable to translate Unicode character
\u00E9 at index 5409 to specified code page.
  at System.Text.EncoderExceptionFallbackBuffer.Fallback(Char charUnknown,
Int32 index)
  at System.Xml.CharEntityEncoderFallbackBuffer.Fallback(Char charUnknown,
Int32 index)
  at System.Text.EncoderFallbackBuffer.InternalFallback(Char ch, Char*& chars)
  at System.Text.ASCIIEncoding.GetBytes(Char* chars, Int32 charCount, Byte*
bytes, Int32 byteCount, EncoderNLS encoder)
  at System.Text.EncoderNLS.Convert(Char* chars, Int32 charCount, Byte* bytes,
Int32 byteCount, Boolean flush, Int32& charsUsed, Int32& bytesUsed, Boolean&
completed)
  at System.Text.EncoderNLS.Convert(Char[] chars, Int32 charIndex, Int32
charCount, Byte[] bytes, Int32 byteIndex, Int32 byteCount, Boolean flush, Int32&
charsUsed, Int32& bytesUsed, Boolean& completed)
  at System.Xml.XmlEncodedRawTextWriter.EncodeChars(Int32 startOffset, Int32
endOffset, Boolean writeAllToStream)
  at System.Xml.XmlEncodedRawTextWriter.FlushBuffer()
  at System.Xml.XmlEncodedRawTextWriter.RawText(Char* pSrcBegin, Char* pSrcEnd)
  at System.Xml.XmlEncodedRawTextWriter.RawText(String s)
  at System.Xml.XmlEncodedRawTextWriter.WriteStartElement(String prefix, String
localName, String ns)
  at System.Xml.XmlWellFormedWriter.WriteStartElement(String prefix, String
localName, String ns)
  at System.Xml.XmlWriter.WriteStartElement(String localName)
 
I thought the whole point of using an XmlWriter was that it would translate the
chars into legal XML.

How can I resolve this?
--
Thanks in advance, Les Caudle
Martin Honnen - 20 Jun 2007 16:05 GMT
> I've got some C# 2.0 code that has been working for a year.
>
[quoted text clipped - 33 lines]
> I thought the whole point of using an XmlWriter was that it would translate the
> chars into legal XML.

Are you perhaps trying to write out XML in an encoding like US-ASCII
that does not include the character 'é'?
What are you XmlWriterSettings exactly when you get that error?

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Les Caudle - 20 Jun 2007 19:49 GMT
>> I've got some C# 2.0 code that has been working for a year.
>>
[quoted text clipped - 37 lines]
>that does not include the character 'é'?
>What are you XmlWriterSettings exactly when you get that error?

Martin - Yes, I'm using Encoding.ASCII.

If I output to UTF8 encoding, all is fine, but NetSuite will no longer import
the XML.  So, I need to find a way to automate creating the XML formatted as
ASCII.

Here are the settings:

settings.NewLineChars = @"\r\n";
settings.NewLineHandling = NewLineHandling.Replace;
settings.NewLineOnAttributes = true;
settings.Encoding = Encoding.ASCII;

Thanks, Les Caudle
Martin Honnen - 21 Jun 2007 13:12 GMT
> Martin - Yes, I'm using Encoding.ASCII.
>
[quoted text clipped - 8 lines]
> settings.NewLineOnAttributes = true;
> settings.Encoding = Encoding.ASCII;

Any XML parser/application is supposed to support UTF-8 and UTF-16 but
not ASCII.
I don't think XmlWriter can do what you want unless your code manually
makes sure it writes out non-ASCII characters as character references
e.g. like this

        xmlWriter.WriteStartElement("foo");
        foreach (char c in "Je suis fatigué.") {
          if (c < 128) {
            xmlWriter.WriteString(c.ToString());
          }
          else {
            xmlWriter.WriteCharEntity(c);
          }
        }
        xmlWriter.WriteEndElement();

that then results in

<foo>Je suis fatigu&#xE9;.</foo>

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Martin Honnen - 21 Jun 2007 14:13 GMT
> Any XML parser/application is supposed to support UTF-8 and UTF-16 but
> not ASCII.
> I don't think XmlWriter can do what you want unless your code manually
> makes sure it writes out non-ASCII characters as character references

Another solution could be to generate the XML in UTF-8 or UTF-16 first,
then to apply an XSLT styleshet like this

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="no" encoding="US-ASCII"/>
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

with .NET XslCompiledTransform. That way .NET seems to be able to ensure
the output is US-ASCII by using character references as needed for
characters that are outside of the target encoding.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

WenYuan Wang [MSFT] - 22 Jun 2007 12:44 GMT
Hello Les Caudle,

I totally agree with Martin.

ASCIIEncoding object cannot encode a character whose Unicode code point
value is outside the range U+0000 to U+007F.
According to the error message you pasted, WriteStartElement() method want
to write an illegal character (\u00E9 ¨¦) into ASCII encoded XML file. Thus
.net runtime throw an EncoderFallbackException.

Have you tried Martin's suggestion so far? Does this method works for you?
If you face any further issue, please update here. Thus we could follow up.
:)

Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

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.