Alex Rivera | Logout

Insert Unicode data from xml string to Datatable

Asked 2012-02-06T05:12:19.670
11

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.

Edit
Report

1 Answer

2

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.

answered 2012-02-06T07:57:50.903

Your Answer