Results 1 to 3 of 3

Thread: [RESOLVED] I cannot get correct encoding on xml serialization.

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Resolved [RESOLVED] I cannot get correct encoding on xml serialization.

    Hi.
    I am using a class to convert to XML.
    The issue |I am having is that the encoding of the XMLRoot is giving strange symbols if I use ,p.e. "http://mybank.gr/paycenter/redirection"
    It will turn our as: http_x003A__x002F__x002F_mybank.gr_x002F_paycenter_x002F_redirection

    I've tried to create an ExtentedStringWriter so I can pass any encoding but anything I use does not work.
    Maybe I need to use some other text manip class?

    So here is the code:

    Code:
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim ser As New Bank
            ser.GroundClearance = "sd"
            ser.SeatingCapacity = "ff"
    
            Dim serializer As New XmlSerializer(ser.GetType)     
            Dim strxml As String
            Dim stringbuild As New StringBuilder
    
            Using texttWriter As ExtentedStringWriter = New ExtentedStringWriter(stringbuild, Encoding.ASCII)
                serializer.Serialize(texttWriter, ser)
                strxml = texttWriter.ToString()
            End Using
    
        End Sub
    
    
    
    Public NotInheritable Class ExtentedStringWriter
        Inherits StringWriter
    
        Private ReadOnly stringWriterEncoding As Encoding
    
        Public Sub New(ByVal builder As StringBuilder, ByVal desiredEncoding As Encoding)
            MyBase.New(builder)
            Me.stringWriterEncoding = desiredEncoding
        End Sub
    
        Public Overrides ReadOnly Property Encoding As Encoding
            Get
                Return Me.stringWriterEncoding
            End Get
        End Property
    End Class
    
    
    <XmlRoot(ElementName:="http://mybank.gr/paycenter/redirection")>
    Public Class Car
        <XmlElement(ElementName:="GroundClearance")>
        Public Property GroundClearance() As String
            Get
                Return m_GroundClearance
            End Get
            Set(value As String)
                m_GroundClearance = value
            End Set
        End Property
        Private m_GroundClearance As String
        <XmlElement(ElementName:="Capacity")>
        Public Property Capacity() As String
            Get
                Return m_Capacity
            End Get
            Set(value As String)
                m_Capacity = value
            End Set
        End Property
        Private m_Capacity As String
    End Class
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: I cannot get correct encoding on xml serialization.

    Are you sure that this:
    vb.net Code:
    1. <XmlRoot(ElementName:="http://mybank.gr/paycenter/redirection")>
    shouldn't be this:
    vb.net Code:
    1. <XmlRoot(Namespace:="http://mybank.gr/paycenter/redirection")>
    I'm no XML expert but I wouldn't expect colon or forward slash characters to be valid in an element name. Those names are identifiers, like type or variable names in VB.

    https://www.w3schools.com/xml/xml_elements.asp

    From the XML Naming Rules section:
    Element names can contain letters, digits, hyphens, underscores, and periods
    Nothing to do with encoding.

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: I cannot get correct encoding on xml serialization.

    You are absolutely correct JMC.
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width