Hi,
could u please give me an idea how can i convert xml file with UTF-8 encode into UTF-16 encoding in c#.
thanks in advance,
jitendra
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
jit sunPosted Feb 17, 2012, 10:07 AM
I got solution to convert into utf-16 with dtds.
here is the code:
var filePath = Application.StartupPath;
string[] filePaths = Directory.GetFiles(filePath, "*.xml"); // get all the xml files from relative folder
foreach (string xyz in filePaths) // Take each xml file from folder
{
string filepath = xyz;
StreamReader fileStream = new StreamReader(filepath);
string fileContent = fileStream.ReadToEnd();
fileStream.Close();
StreamWriter ansiWriter = new StreamWriter(filepath.Replace(".xml", "-unic.xml"), false, Encoding.Unicode);
ansiWriter.Write(fileContent);
ansiWriter.Close();
}
Thanks for you support
Jit
VulpesPosted Jan 19, 2012, 5:25 PM
If you can look up what unicode characters they represent, then you'd be better replacing them with those characters in the XML file before you try to load it and do the conversion.
The multiplication sign is character 00d7 hex or 215 decimal.
It's easy to do this programatically and they're a security risk in any case.
jit sunPosted Jan 19, 2012, 10:59 AM
actually i tried like you(replaced with space) and it s working.
is there any other solution to this problem?
thanks allot
Jitendra
VulpesPosted Jan 19, 2012, 10:44 AM
&SGV;
×
The second of these is an HTML entity for the multiplication sign but I have no idea what the first one is :-/
Anyway, I removed the first one altogether and replaced the second one with 'x' and it then loaded and converted fine :)
jit sunPosted Jan 19, 2012, 10:16 AM
it is giving me exception like "Reference to undeclared entity 'SGV'. Line 24, position 95." at the code line var doc = XDocument.Load(utf8FilePath);
Jitendra Sunkara
jit sunPosted Jan 19, 2012, 9:38 AM
go through attachment.
jit sunPosted Jan 19, 2012, 9:34 AM
here is my xml code.
VulpesPosted Jan 19, 2012, 9:04 AM
You've forgotten to attach the file.
If you just post the declaration section, that should be enough for me to figure out why it's not working.
jit sunPosted Jan 19, 2012, 8:29 AM
thanks alot. it is working for some xml files.
I try to load a some other xml files. But it is giving error.
Please go through attachment and u will find my xml document.
jitendra
VulpesPosted Jan 19, 2012, 7:14 AM
jit sunPosted Jan 19, 2012, 7:05 AM
thanks alot.
i have already an xml file on my desktop with utf-8 and i want to change it to utf-16.
could you please write the source code.
jitendra
VulpesPosted Jan 19, 2012, 6:16 AM
Here's some very simple LINQ to XML code which shows how to do this:
// create utf-8 file
The size of the file increased from 77 to 152 bytes as a result of this manoeuvre because, of course, utf-16 requires 2 bytes per character as opposed to one byte for characters in the ASCII range when utf-8 is used.