ISO-8859-1 encoding of an xml string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christina

    ISO-8859-1 encoding of an xml string

    Hey Guys,

    Currently, I am using the below code:

    Dim oReqDoc as XmlDocument
    Dim requiredBytes As Byte()
    requiredBytes =
    System.Text.UTF 8Encoding.UTF8. GetBytes(oReqDo c.InnerXml).

    Here, I am encoding my xml string in UTF8 format.
    I want to convert my code to encode the xml string in 'ISO-8859-1'
    format.

    Any help is appreciated..

    Thx,
    Chris

  • Branco Medeiros

    #2
    Re: ISO-8859-1 encoding of an xml string


    Christina wrote:
    <snip>
    Dim oReqDoc as XmlDocument
    Dim requiredBytes As Byte()
    requiredBytes =
    System.Text.UTF 8Encoding.UTF8. GetBytes(oReqDo c.InnerXml).
    >
    Here, I am encoding my xml string in UTF8 format.
    I want to convert my code to encode the xml string in 'ISO-8859-1'
    format.
    <snip>

    Use the GetEncoding method of the Encoding class to get a custom
    encoding.
    Dim oReqDoc as XmlDocument
    Dim requiredBytes As Byte()
    Dim E As System.Text.Enc oding
    E = System.Text.Enc oding.GetEncodi ng("ISO-8859-1")
    requiredBytes = E.GetBytes(oReq Doc.InnerXml)


    HTH,

    Branco.

    Comment

    • Christina

      #3
      Re: ISO-8859-1 encoding of an xml string

      Thanks Branco !!
      Thats was helpful. I still have another question.

      I made the changes. Now, when I debug the application and check at
      location where I posted the xml, Request.content Encoding has value
      System.Text.UTF 8Encoding.

      1) Is it because of the web.configs default :
      <globalizatio n requestEncoding ="utf-8" responseEncodin g="utf-8" />

      2) How can I check that the message is ISO-8859-1 encoded ?

      Thanks again..
      Chris

      Branco Medeiros wrote:
      Christina wrote:
      <snip>
      Dim oReqDoc as XmlDocument
      Dim requiredBytes As Byte()
      requiredBytes =
      System.Text.UTF 8Encoding.UTF8. GetBytes(oReqDo c.InnerXml).

      Here, I am encoding my xml string in UTF8 format.
      I want to convert my code to encode the xml string in 'ISO-8859-1'
      format.
      <snip>
      >
      Use the GetEncoding method of the Encoding class to get a custom
      encoding.
      >
      Dim oReqDoc as XmlDocument
      Dim requiredBytes As Byte()
      >
      Dim E As System.Text.Enc oding
      E = System.Text.Enc oding.GetEncodi ng("ISO-8859-1")
      requiredBytes = E.GetBytes(oReq Doc.InnerXml)
      >
      >
      HTH,
      >
      Branco.

      Comment

      • Branco Medeiros

        #4
        Re: ISO-8859-1 encoding of an xml string

        Christina wrote:
        <snip>
        I made the changes. Now, when I debug the application and check at
        location where I posted the xml, Request.content Encoding has value
        System.Text.UTF 8Encoding.
        >
        1) Is it because of the web.configs default :
        <globalizatio n requestEncoding ="utf-8" responseEncodin g="utf-8" />
        Uh, I guess you answered your question...
        2) How can I check that the message is ISO-8859-1 encoded ?
        Appart from visually inspecting the contents of the generated file in a
        binary editor (!!!), I have no idea. ISO-8859-1 has no preamble bytes
        as do utf-8 ~ utf-32, so there's no 'mark' you can look for, other than
        the distinction between certain char conversions... for instance, utf-8
        will turn any char with index above 127 into a two byte sequence, while
        Latin 1 will convert such char to a single byte.

        Therefore Text.Encoding.U TF8.GetByteCoun t(ChrW(128)) will return 2,
        while Text.Encoding.G etEncoding("iso-8859-1").GetByteCoun t(ChrW(128))
        will return 1... =P

        HTH...

        Regards,

        Branco.

        Comment

        • Michel Posseth [MCP]

          #5
          Re: ISO-8859-1 encoding of an xml string


          write your xml with a encoded string writer


          Imports System.Text
          Imports System.IO
          Namespace Text
          '''<summary>
          ''' Implementeerd een stringwriter waarbij je zelf de encoding kunt
          kiezen
          ''' de data wordt opgeslagen in een stringbuilder
          ''' </summary>
          Public Class EncodedStringWr iter
          Inherits StringWriter
          'Private property setter
          Private _Encoding As Encoding
          ''' <summary>
          ''' Initializes a new instance of the <see
          cref="T:Encoded StringWriter" /class.
          ''' </summary>
          ''' <param name="sb">strin gbuilder</param>
          ''' <param name="Encoding" >The encoding.</param>
          Public Sub New(ByVal sb As StringBuilder, ByVal Encoding As Encoding)
          MyBase.New(sb)
          _Encoding = Encoding
          End Sub
          ''' <summary>
          '''
          ''' </summary>
          ''' <value></value>
          ''' <doc>
          ''' <summary>gett er voor de string encoding </summary>
          ''' <returns>de Encoding waarin wordt geschreven </returns>
          ''' <filterpriority >1</filterpriority>
          ''' </doc>
          Public Overrides ReadOnly Property Encoding() As Encoding
          Get
          Return _Encoding
          End Get
          End Property
          End Class
          End Namespace

          USAGE ::::

          Mvar_sb = New StringBuilder(5 00)
          Mvar_XMLDoc = New XmlTextWriter(N ew
          Text.EncodedStr ingWriter(Mvar_ sb, Encoding.UTF8))
          ------ choose your encoding

          ------'now write xml
          Mvar_XMLDoc.For matting = Formatting.Inde nted
          Mvar_XMLDoc.Ind entation = 2
          Mvar_XMLDoc.Wri teStartDocument ()
          ------omitted

          ------to get the contents of the xml document
          Mvar_sb.ToStrin g

          as you see everything is now in the requested encoding also the doctype is
          now in the correct encoding


          regards

          Michel Posseth [MCP]











          "Branco Medeiros" wrote:
          Christina wrote:
          <snip>
          I made the changes. Now, when I debug the application and check at
          location where I posted the xml, Request.content Encoding has value
          System.Text.UTF 8Encoding.

          1) Is it because of the web.configs default :
          <globalizatio n requestEncoding ="utf-8" responseEncodin g="utf-8" />
          >
          Uh, I guess you answered your question...
          >
          2) How can I check that the message is ISO-8859-1 encoded ?
          >
          Appart from visually inspecting the contents of the generated file in a
          binary editor (!!!), I have no idea. ISO-8859-1 has no preamble bytes
          as do utf-8 ~ utf-32, so there's no 'mark' you can look for, other than
          the distinction between certain char conversions... for instance, utf-8
          will turn any char with index above 127 into a two byte sequence, while
          Latin 1 will convert such char to a single byte.
          >
          Therefore Text.Encoding.U TF8.GetByteCoun t(ChrW(128)) will return 2,
          while Text.Encoding.G etEncoding("iso-8859-1").GetByteCoun t(ChrW(128))
          will return 1... =P
          >
          HTH...
          >
          Regards,
          >
          Branco.
          >
          >

          Comment

          Working...