简体   繁体   English

VB.NET反序列化/应该使用哪种模式?

[英]VB.NET deserialization / Which pattern should i use?

I want to map a given XML in object form. 我想以对象形式映射给定的XML。 The XML includes eg values ​​like a file path. XML包含例如文件路径之类的值。 A typical path consists of an xml-attribute for the folder-path and a file-name. 典型的路径由用于文件夹路径的xml属性和文件名组成。 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.) (我不想更改原始的序列化对象(例如“ katalog”,“ kovkatalog”),因为它是自动生成的。)

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." “不希望InvalidOperationException:http://example.net/V3.0/Schema'>。”

Does anyone have a solution for how I can extend inheritance with additional methods? 有谁对我如何使用其他方法扩展继承有解决方案?

VB.NET-CODE: 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: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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