Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to change the encoding type of XML file to UTF-8. I tried reading the file and write it back with UTF-8 but its written an empty file back, Because I dont know the default encoding of a file.

Kindly help me out.

regards
Posted
Comments
Sushil Mate 24-Sep-12 2:39am    
show us the code?
Ahamed Azeem 24-Sep-12 2:45am    
File.WriteAllText(ev.FullPath, File.ReadAllText(ev.FullPath, Encoding.Default), Encoding.UTF8);

This is the code which i am trying to read and write it back, which is working fine in my local machine but when i deploy the application on the server, it writes back an empty file. I think problem in encoding.default.

Kindly suggest
Ahamed Azeem 24-Sep-12 4:36am    
Is it possible to change the encoding type programmatically

1 solution

Are you saying the XML you have (the source) does not specify an Encoding Type ?

Have you tried examining the Encoding Type of the XmlDocument your source is (hopefully) read into using : XmlDeclaration.Encoding get/set Property: what happens if you do ?

If your source XML is .NET generated, and does not specify an encoding attribute: UTF-8 will be the default in either reading or writing out the XML.

Assuming you have read in the XML using 'StringReader or whatever, and you have a valid 'XmlDocument created, when writing it out, as UTF-8, you should create an XmlDeclaration instance, set the XmlEncoding property of that instance, and add it to the root node of the XmlDocument.

This is shown in the code example here on MSDN:[^].

Now if your source XML is in some weird format, that you don't know, and can't determine: perhaps that mean you are going to have to figure out its structure yourself and use an XSLT to get into a usable form ?

good luck, Bill
 
Share this answer
 
Comments
Ahamed Azeem 24-Sep-12 8:49am    
Source XML is not .net generated. How can i find out the encoding type, I tried reading through encoding.default and write it with UTF8, its succeeded in my local machine and failed in server. Kindly advise
BillWoodruff 24-Sep-12 9:29am    
First, have you opened the source file in a text editor, and found there is an XmlDeclaration element before its root element ? If so, what is it ?

Assuming you have been able to read it in C#, and create a valid XmlDocument, then what, exactly, does the XmlDeclaration.Encoding property of that document's DOM return ?

I suggest you modify your question to describe exactly how it is "failing on the server:" that it works "locally," but, not on the server: that's very important information, and, unfortunately, we're not psychics :)

Be sure and specify what the server is you are talking about: ASP.NET ? Try to very precisely describe if there is any variation in the way the code is used or invoked on the server compared to your local machine.

I am not competent in the area of server-side questions, but I would immediately wonder if the server-side technology, assuming ASP.NET, has some global settings that affect encoding.

good luck, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900