如何使用VB.NET显示注册商标符号(®)?

时间:2010-09-28 18:14:33

标签: vb.net encoding ascii xmltextwriter

我已经接管了我工作的公司的申请表。突然之间,客户希望能够在名称字段中输入注册商标。使用ALT + 0174的符号正在正确保存,但应用程序未正确编码。基本上这就是我正在做的事情:

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Dim ms As New MemoryStream
    Dim xml_text_writer As New XmlTextWriter(ms, _
        System.Text.Encoding.UTF8)

    xml_text_writer.Formatting = Formatting.Indented
    xml_text_writer.Indentation = 4

    xml_text_writer.WriteStartDocument(True)
    xml_text_writer.WriteStartElement("Employees")

    MakeEmployee(xml_text_writer, txtFName.Text, txtLName.Text, 11111)

    xml_text_writer.WriteEndElement()
    xml_text_writer.WriteEndDocument()
    xml_text_writer.Flush()

    ms.Flush()
    ms.Position = 0
    Dim b(CInt(ms.Length) - 1) As Byte
    ms.Read(b, 0, CInt(ms.Length))
    ms.Close()

    Dim strReturn As String = System.Text.ASCIIEncoding.ASCII.GetString(b)
    If strReturn.StartsWith("???", StringComparison.CurrentCultureIgnoreCase) Then
        strReturn = strReturn.Substring(3)
    End If

    txtName.Text = strReturn

End Sub

Private Sub MakeEmployee(ByVal xml_text_writer As  _
XmlTextWriter, ByVal first_name As String, ByVal _
last_name As String, ByVal emp_id As Integer)
    xml_text_writer.WriteStartElement("Employee")

    xml_text_writer.WriteStartElement("FirstName")
    xml_text_writer.WriteString(first_name)
    xml_text_writer.WriteEndElement()

    xml_text_writer.WriteStartElement("LastName")
    xml_text_writer.WriteString(last_name)
    xml_text_writer.WriteEndElement()

    xml_text_writer.WriteStartElement("EmployeeId")
    xml_text_writer.WriteString(emp_id.ToString)
    xml_text_writer.WriteEndElement()

    xml_text_writer.WriteEndElement()
End Sub

我从这段代码得到的输出是:

             约翰         史密斯?         11111     

一切都很好,除了“??”在“史密斯”之后应该是“®”。如果有的话,正确显示符号的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

  

Dim strReturn As String = System.Text.ASCIIEncoding.ASCII.GetString(b)

您使用UTF-8编码XML,不要将其作为ASCII读回。您将丢失所有无法编码的特殊字符。修正:

Dim strReturn As String = System.Text.Encoding.UTF8.GetString(b)