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 144601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:16:45+00:00 2026-05-11T08:16:45+00:00

To load XML files with arbitrary encoding I have the following code: Encoding encoding;

  • 0

To load XML files with arbitrary encoding I have the following code:

Encoding encoding; using (var reader = new XmlTextReader(filepath)) {     reader.MoveToContent();     encoding = reader.Encoding; }  var settings = new XmlReaderSettings { NameTable = new NameTable() }; var xmlns = new XmlNamespaceManager(settings.NameTable); var context = new XmlParserContext(null, xmlns, '', XmlSpace.Default,      encoding); using (var reader = XmlReader.Create(filepath, settings, context)) {     return XElement.Load(reader); } 

This works, but it seems a bit inefficient to open the file twice. Is there a better way to detect the encoding such that I can do:

  1. Open file
  2. Detect encoding
  3. Read XML into an XElement
  4. Close file
  • 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. 2026-05-11T08:16:46+00:00Added an answer on May 11, 2026 at 8:16 am

    Ok, I should have thought of this earlier. Both XmlTextReader (which gives us the Encoding) and XmlReader.Create (which allows us to specify encoding) accepts a Stream. So how about first opening a FileStream and then use this with both XmlTextReader and XmlReader, like this:

    using (var txtreader = new FileStream(filepath, FileMode.Open)) {     using (var xmlreader = new XmlTextReader(txtreader))     {         // Read in the encoding info         xmlreader.MoveToContent();         var encoding = xmlreader.Encoding;          // Rewind to the beginning         txtreader.Seek(0, SeekOrigin.Begin);          var settings = new XmlReaderSettings { NameTable = new NameTable() };         var xmlns = new XmlNamespaceManager(settings.NameTable);         var context = new XmlParserContext(null, xmlns, '', XmlSpace.Default,                  encoding);          using (var reader = XmlReader.Create(txtreader, settings, context))         {             return XElement.Load(reader);         }     } } 

    This works like a charm. Reading XML files in an encoding independent way should have been more elegant but at least I’m getting away with only one file open.

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

Sidebar

Related Questions

I'm trying to use the following code # LOAD XML FILE $XML = new
I have following piece of code which tries to load an XML file from
Background I have inherited a load of XML files that consistently contain a tag
For a new project I need to load big XML files (200MB+) to a
The following code needs to produce an XML file. string path = @c:\load\myFile.xml; SqlConnection
Currently I'm using PHP to load multiple XML files from around the web (non-local)
I'm in a new project where I need to load XML files from other
Im a new web developer trying to load some xml files into a JSP
I am using ElementTree to load up a series of XML files and parse
Using php I want to load XML files from several websites and want to

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.