Reputation: 4097
I have a database in ISO-8859-2 format, but I need to create XML in UTF-8. This means that I must encode the database before printing in UTF-8. I know very little about ASP.Net.
In PHP I would do something like this:
db_connect();
mysql_query("SET NAMES 'UTF8'");
mysql_query("SET character_set_client='UTF8'");
This is my ASP.Net code for database connection:
'CONNECTION TO DATABASE
dim dbconn,sql,dbcomm
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("../baze/test.mdb"))
dbconn.Open()
sql="SELECT * FROM nekretnine, tipovinekretnina WHERE nekretnine.idtipnekretnine = tipovinekretnina.idtipnekretnine ORDER BY nekretnine.idnekretnine"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
while dbread.Read()
Where and how do I encode to UTF-8?
Upvotes: 0
Views: 2033
Reputation: 11980
Assuming you have a value string in str
, this is the pure-.NET way of doing this.
var encoding = System.Text.Encoding.GetEncoding("iso-8859-2");
var bytes = System.Text.Encoding.Convert(encoding, System.Text.Encoding.Default, encoding.GetBytes(str));
var newString = System.Text.Encoding.Default.GetString(bytes);
Upvotes: 2
Reputation: 9802
The .NET Framework's internal string type is UTF-16. All database access will convert to UTF-16 so that you can view the data appropriately: the database, or the OLE DB provider, will convert to UTF-16.
The XML writer classes (you are using XmlDocument or XmlWriter, right?) will then convert to UTF-8 on the output.
Basically, you shouldn't need to do anything extra.
Upvotes: 1