Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read XML file in Encoding utf-8

Tags:

c#

xml

I'm trying to read an XML file which has some Chinese characters in it. While writing the information in the file, encoding UTF-8 works properly and Chinese characters are written in the file properly. But when I try to read it it shows some ????? instead.

I'm just wondering if there is anyone who has already encountered whit this problem and can give me some clue.

like image 537
Shanoo Avatar asked Sep 19 '25 15:09

Shanoo


1 Answers

First, please make sure the data is actually readable in UTF8, the way you do this is:

  • Download a notepad that lets you specify the encoding being used to view the document like Notepad2.
  • Open your document in Notepad2
  • File -> Encoding -> UTF8

If you see the same garbled text, you did not actually create UTF8 encoded xml, but something else.

Back to your question:
Since you don't give us a lot of info how you parse the XML in the first place here is an example of how you would specifically parse it with UTF8:

var xmlDoc = XDocument.Parse(
                 File.ReadAllText("filelocation", System.Text.Encoding.UTF8));

The XDocument class is part for the System.Xml.Linq namespace.

Also if you want to optimize this you might not want pass in a stream rather than the string containing the entire xml document.

like image 53
ntziolis Avatar answered Sep 22 '25 05:09

ntziolis