Welcome to Sol 3 Sign in | Join | Help
CS Search | Live Search Search

Sol 3

Home of Barrows Software Solutions, LLC

Keith Barrows

Subjects range from Personal to Technical.

Loading XML with accented characters breaks System.XmlDocument.Load()

It took me a bit of time but I finally found a solution for loading a XML file that has accented characters (like áéíóúâä) into a UTF-8 format.  I'm loading data for a client and it ended up having a name in it with an accented e character.  For the first time on this project System.XmlDocument.Load() was blowing up.  With a lot of Googling I finally found a link that gave me, what I hope, is the solution for this problem.  For now it is working so I'll go with it.  The link to the article I found is in the code sample below as well as the image to the left.  The magic happens by reading in while enforcing a double-byte encoding then saving out in an encoding that gives the visual representation we, in the US, would expect.

Hope this helps someone else.


   1:  #region Fix Character Encodings
   2:  // Need to drop accented characters back to normal characters...
   3:  // reference:  http://www.codeproject.com/KB/cs/EncodingAccents.aspx //
   4:  StreamReader sr = new StreamReader(_pfInfo.WorkingFile, Encoding.GetEncoding("iso-8859-1"));
   5:  string fileContents = sr.ReadToEnd();
   6:  sr.Close();
   7:  sr = null;
   8:   
   9:  StreamWriter sw = new StreamWriter(_pfInfo.WorkingFile, false, Encoding.GetEncoding("iso-8859-8"));
  10:  sw.Write(fileContents);
  11:  sw.Flush();
  12:  sw.Close();
  13:  sw = null;
  14:  #endregion
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
Published Friday, September 26, 2008 6:29 PM by Keith Barrows
Filed under: , , , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

ASPInsiders said:

It took me a bit of time but I finally found a solution for loading a XML file that has accented characters

September 26, 2008 7:04 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Keith Barrows

I've been in computing since 1975. I started on an old PDP-8J with 3k of memory and 2 teletypes. I learned BASIC and Octal based assembly. I later moved into CPM, TR-DOS, Apple and finally into PC-DOS, Dr DOS and MS-DOS. I've been a beta tester for over a decade, got into web applications as a means to handle B2B requirements and have specialized in data movement between applications and businesses since. I have been a MVP, ASP Elite and was selected by Microsoft as one of the original 15 board members for ASPInsiders.
CS Build: 2.1.61129.2
1999
Listed on the CS Listings Powered By Community Server Themed by nb development