views:

1048

answers:

4

I have database in iso-8859-2 format. But I need to create XML in utf-8. I almost don't know anything about asp.net so I need HELP! I have database in iso-8859-2, but I need to create xml file in utf-8. That means tha I must encode database before prinitng in UTF-8! Thanks In PHP goes 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 now encode? Thanks

A: 

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.

Mike Dimmick
+1  A: 

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);
Omer van Kloeten
A: 

I tryed like this, but it doesn't work! Where I go wrong?

[code] while dbread.Read() Dim encoding AS string = System.Text.Encoding.GetEncoding("iso-8859-2");

   Dim bytes AS string = System.Text.Encoding.Convert(encoding, System.Text.Encoding.Default, encoding.GetBytes(dbread("opis")));

   Dim newString As string = System.Text.Encoding.Default.GetString(bytes);

[/code]

Wizard4U
A: 

I've changed im this, but it still doesn't convert: PLEASE HELP dim dbconn,sql,dbcomm,mysql_query dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("../baze/vitus.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()

 Dim encoding = System.Text.Encoding.GetEncoding("iso-8859-2")

  while dbread.Read()

   Dim bytes = System.Text.Encoding.Convert(encoding, System.Text.Encoding.Default, encoding.GetBytes(dbread("opis")))

   Dim newString As string = System.Text.Encoding.Default.GetString(bytes)
Wizard4U