lundi 15 juillet 2019

VB.NET deserialization / Which pattern should i use?

I want to map a given XML in object form. The XML includes e.g. values ​​like a file path. A typical path consists of an xml-attribute for the folder-path and a file-name. The pattern should give me the opportunity to load and save object at any time. I don't want to put the file path always manually together or separate them. (I don't want to change the original serialized-object (like "katalog","kovkatalog",) because it is automatically generated.)

I think inheritance could solve my problem and improve the overviewability of my code (or should I use an other wrapper?). Is something against the approach? Are there any better approaches?

When deserializing, the following error is always returned:

"InvalidOperationException: <katalog xmlns='http://example.net/V3.0/Schema'> is not expected."

Does anyone have a solution for how I can extend inheritance with additional methods?

VB.NET-CODE:

Namespace abc
    Public Class Main
        Public Sub New()
            Dim des As New unifa_katalog
            des = Deserialisieren(Of unifa_katalog)("<PATH_TO_FILE>\katalog.xml")
        End Sub

        Private Function Deserialisieren(Of T)(strSpeicherOrt As String) As T
            Dim serializer As New XmlSerializer(GetType(T))
            Dim deserialized As T = Nothing
            Using file = System.IO.File.OpenRead(strSpeicherOrt)
                deserialized = DirectCast(serializer.Deserialize(file), T)
            End Using
            Return deserialized
        End Function
    End Class

    <XmlRoot("katalog", [Namespace]:="urn:kosxmlns")>
    Partial Public Class unifa_katalog
        Inherits katalog
        Public Sub furtherMethod()

        End Sub

    End Class


    <System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432"),
 System.SerializableAttribute(),
 System.Diagnostics.DebuggerStepThroughAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://example.net/V3.0/Schema"),
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="http://example.net/V3.0/Schema", IsNullable:=False)>
    Partial Public Class katalog
        Inherits kovkatalog
    End Class
    Partial Public Class kovkatalog

        Private verfahrenField As String
        Private verfahrensversionField As String
        Private katalogversionField As Integer
        Private myversionField As String

        Public Sub New()
            MyBase.New
            Me.katalogversionField = 1
        End Sub


        '''<remarks/>
        <System.Xml.Serialization.XmlAttributeAttribute()>
        Public Property verfahren() As String
            Get
                Return Me.verfahrenField
            End Get
            Set
                Me.verfahrenField = Value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlAttributeAttribute("verfahrens-version")>
        Public Property verfahrensversion() As String
            Get
                Return Me.verfahrensversionField
            End Get
            Set
                Me.verfahrensversionField = Value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlAttributeAttribute("katalog-version"),
     System.ComponentModel.DefaultValueAttribute(1)>
        Public Property katalogversion() As Integer
            Get
                Return Me.katalogversionField
            End Get
            Set
                Me.katalogversionField = Value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlAttributeAttribute("my-version")>
        Public Property myversion() As String
            Get
                Return Me.myversionField
            End Get
            Set
                Me.myversionField = Value
            End Set
        End Property
    End Class
End Namespace

XMLFILE:

<?xml version="1.0" encoding="UTF-8"?>
<kov:katalog verfahren="kov" my-version="03000200" katalog-version="1" verfahrens-version="06090000" xmlns:kov="http://example.net/V3.0/Schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</kov:katalog>

Aucun commentaire:

Enregistrer un commentaire