Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9112071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:38:53+00:00 2026-06-17T03:38:53+00:00

I faced a problem with reading the XML. The solution was found, but there

  • 0

I faced a problem with reading the XML. The solution was found, but there are still some questions. The incorrect XML file is in encoded in UTF-8 and has appropriate mark in its header. But it also includes a char encoded in UTF-16 – ‘é’. This code was used to read XML file for validating its content:

var xDoc = XDocument.Load(taxFile);

It raises exception for specified incorrect XML file: “Invalid character in the given encoding. Line 59, position 104.” The quick fix is as follows:

XDocument xDoc = null;
using (var oReader = new StreamReader(taxFile, Encoding.UTF8))
{
    xDoc = XDocument.Load(oReader);
}

This code doesn’t raise exception for the incorrect file. But the ‘é’ character is loaded as �. My first question is “why does it work?”.

Another point is using XmlReader doesn’t raise exception until the node with ‘é’ is loaded.

XmlReader xmlTax = XmlReader.Create(filePath);

And again the workout with StreamReader helps. The same question.
It seems like the fix solution is not good enough, cause one day 🙂 XML encoded in another format may appear and it could be proceed in the wrong way. BUT I’ve tried to process UTF-16 formatted XML file and it worked fine (configured to UTF-8).

The final question is if there are any options to be provided for XDocument/XmlReader to ignore characters encoding or smth like this.

Looking forward for your replies. Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:38:54+00:00Added an answer on June 17, 2026 at 3:38 am

    The first thing to note is that the XML file is in fact flawed – mixing text encodings in the same file like this should not be done. The error is even more obvious when the file actually has an explicit encoding embedded.

    As for why it can be read without exception with StreamReader, it’s because Encoding contains settings to control what happens when incompatible data is encountered

    Encoding.UTF8 is documented to use fallback characters. From http://msdn.microsoft.com/en-us/library/system.text.encoding.utf8.aspx:

    The UTF8Encoding object that is returned by this property may not have
    the appropriate behavior for your application. It uses replacement
    fallback to replace each string that it cannot encode and each byte
    that it cannot decode with a question mark (“?”) character.

    You can instantiate the encoding yourself to get different settings. This is most probably what XDocument.Load() does, as it would generally be bad to hide errors by default.
    http://msdn.microsoft.com/en-us/library/system.text.utf8encoding.aspx

    If you are being sent such broken XML files step 1 is to complain (loudly) about it. There is no valid reason for such behavior. If you then absolutely must process them anyway, I suggest having a look at the UTF8Encoding class and its DecoderFallbackProperty. It seems you should be able to implement a custom DecoderFallback and DecoderFallbackBuffer to add logic that will understand the UTF-16 byte sequence.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

today I faced with very interesting problem. When I try to rewrite xml file.
I faced a problem while using threading for the first time, In an SWT
I faced a problem - I need to use a macro value both as
I faced a problem regarding loading ResourceBundle at server-side on Seam 2.2 application. In
I faced a problem that I can not find a way to change the
I have faced a problem in Qt with error: cannot convert parameter 1 from
I recently faced a problem with determining browsers' support for certain DOM features. One
We've faced strange problem. We have log on service, that authenticates user, adds auth
I've faced a problem in Qt where I need to draw lines with borders
I develop the site with Day CQ5 and was faced witha problem. I'm creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.