Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Unicode data from xml string to Datatable

Tags:

c#

xml

unicode

I want to save unicode data into database from xml string by using this code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlText);

using (XmlNodeReader xmlReader = new XmlNodeReader(xmlDoc))
{
        DataTable dt = new DataTable();
        dt.TableName = "sms";
        dt.Columns.Add("rowID");
        dt.Columns.Add("origAddr");
        dt.Columns.Add("time");
        dt.Columns.Add("message");
        dt.ReadXml(xmlReader);
        return dt;
}

but when I save datatable into database my unicode character appear with question mark (???????)

My database collation is correct and other unicode character are stored correctly.

like image 644
Danial Delkhosh Avatar asked Feb 06 '12 05:02

Danial Delkhosh


1 Answers

Usually this happens when you source text is not stored as Unicode. For example, if you read your xml data from a text file, and the text file is stored as Ansi (using codepage), or it is stored as Unicode file without BOM (Byte Order Mark, or signature), when you read your text file, non-ASCII characters may not be read correctly.
To solve this, open your source xml file in a text editor (for example Notepad++) and change your encoding to Unicode or UTF-8, and then save the file.
You can also open the file in Notepad, and save the file as Unicode (File/Save As -> Encoding: Unicode or UTF-8). Make sure that when you open your file in notepad, the characters are displayed correctly.

like image 187
Mohammad Dehghan Avatar answered Oct 16 '22 04:10

Mohammad Dehghan