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? --
[quoted text, click to view] On Wed, 20 Jun 2007 17:05:24 +0200, Martin Honnen <mahotrash@yahoo.de> wrote: >Les Caudle wrote: >> 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. > >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?
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;
[quoted text, click to view] Les Caudle wrote: > 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.
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? -- Martin Honnen --- MVP XML
[quoted text, click to view] Les Caudle wrote: > 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;
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é.</foo> -- Martin Honnen --- MVP XML
[quoted text, click to view] Martin Honnen wrote: > 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. -- Martin Honnen --- MVP XML
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.
Don't see what you're looking for? Try a search.
|