-
Oct 19th, 2021, 05:04 AM
#1
[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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Oct 19th, 2021, 05:16 AM
#2
Re: I cannot get correct encoding on xml serialization.
Are you sure that this:
vb.net Code:
<XmlRoot(ElementName:="http://mybank.gr/paycenter/redirection")>
shouldn't be this:
vb.net Code:
<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.
-
Oct 19th, 2021, 08:02 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|