Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 23-Sep-12 21:22pm
Comments
Sushil Mate at 24-Sep-12 2:39am
   
show us the code?
Ahamed Azeem at 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 at 24-Sep-12 4:36am
   
Is it possible to change the encoding type programmatically

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Ahamed Azeem at 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 at 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)

  Print Answers RSS
0 OriginalGriff 7,205
1 DamithSL 5,114
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,747
4 Kornfeld Eliyahu Peter 4,474


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 24 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100