简体   繁体   中英

VB.NET deserialization / Which pattern should i use?

I want to map a given XML in object form. The XML includes eg 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: 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>

It's not really an inheritance issue, rather a namespace issue. If you use the same namespace, it will deserialize

<XmlRoot("katalog", [Namespace]:="http://example.net/V3.0/Schema")>
Partial Public Class unifa_katalog
    Inherits katalog
    Public Sub furtherMethod()
    End Sub
End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM