Home > Enterprise >  How do I rename a class without breaking the XmlSerializer?
How do I rename a class without breaking the XmlSerializer?

Time:11-02

I am deserializing an XML string to classes, I have posted the entire code below.

<xmlinterface>
<kostenvoranschlaege>
<kostenvoranschlag>
<kvID>1227086</kvID>
<kvRefID/>
<intKvID/>
<status>5</status>
</kostenvoranschlag>
<kostenvoranschlag>
<kvID>1227144</kvID>
<kvRefID/>
<intKvID/>
<status>5</status>
</kostenvoranschlag>
<kostenvoranschlag>
<kvID>1252144</kvID>
<kvRefID/>
<intKvID/>
<status>2</status>
</kostenvoranschlag>
<kostenvoranschlag>

I would like to rename the class name "kostenvoranschlag" to a different name, for example "class kostenvoranschlagVersion1".

However, this breaks the solution. In this line "nKVs.kostenvoranschlaege" count is 0, while normally, it is 16:

For Each k As kostenvoranschlag In nKVs.kostenvoranschlaege

So I thought that it requires this specific name "kostenvoranschlag".

So I added the following to the class:

Public Class kostenvoranschlagVersion1
    <XmlElement("kostenvoranschlag")>

It still does not work.

If I leave the class name intact and just add XMLElement with the same name, it also does NOT work:

Public Class kostenvoranschlag
    <XmlElement("kostenvoranschlag")>

So I guess the XmlElement tag itself is the mistake.

Thank you!

The code:

Imports System.IO
Imports System.Text
Imports System.Xml.Serialization

Public Class Form1

    '<xmlinterface><kostenvoranschlaege><kostenvoranschlag><kvID>1227086</kvID><kvRefID></kvRefID><intKvID></intKvID><status>5</status></kostenvoranschlag><kostenvoranschlag>

    Public Class kostenvoranschlag
        Public Property kvID As Integer
        Public Property kvRefID As String
        Public Property intKvID As String
        Public Property status As Integer
    End Class

    <XmlRoot("xmlinterface")>
    Public Class xmlinterface
        <XmlArray("kostenvoranschlaege")>
        Public kostenvoranschlaege() As kostenvoranschlag
    End Class

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim sXML As String
        sXML = getTheXMLString()
   
        Dim serializer As New XmlSerializer(GetType(xmlinterface))

        Dim xmlstream = New MemoryStream(Encoding.UTF8.GetBytes(sXML))
        xmlstream.Position = 0
        Dim nKVs As xmlinterface = serializer.Deserialize(xmlstream)

        For Each k As kostenvoranschlag In nKVs.kostenvoranschlaege

            If k.status = 2 Then
                Stop
            End If

        Next

        ' For Each Node As Xml.XmlNode In XMLRead.SelectNodes
        Dim breakpointhere = 1

    End Sub
End Class

CodePudding user response:

I got it:

The class kostenvoranschlag does not need an XML type declaration, so it will stay like this:

Public Class kostenvoranschlag_Version1
    Public Property kvID As Integer
    Public Property kvRefID As String
    Public Property intKvID As String
    Public Property status As Integer
End Class

But the parent needs a fix:

<XmlRoot("xmlinterface")>
Public Class xmlinterface
    <XmlArray("kostenvoranschlaege")>
    <XmlArrayItem("kostenvoranschlag")>
    Public kostenvoranschlaege() As kostenvoranschlag_Version1

End Class
  • Related