我想以对象形式映射给定的XML。 XML包括例如值就像文件路径一样。典型的路径由用于文件夹路径的xml属性和文件名组成。该模式应该使我有机会随时加载和保存对象。我不想总是手动将文件路径放在一起或将它们分开。 (我不想更改原始的序列化对象(例如“ katalog”,“ kovkatalog”),因为它是自动生成的。)
我认为继承可以解决我的问题并提高代码的可概述性(或者应该使用其他包装器?)。有什么不对的方法吗?有没有更好的方法?
反序列化时,始终返回以下错误:
“ InvalidOperationException:不需要http://example.net/V3.0/Schema'>。”
有人对我如何使用其他方法扩展继承有解决方案吗?
VB.NET代码:
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>
答案 0 :(得分:1)
这实际上不是继承问题,而是名称空间问题。如果使用相同的名称空间,它将反序列化
<XmlRoot("katalog", [Namespace]:="http://example.net/V3.0/Schema")>
Partial Public Class unifa_katalog
Inherits katalog
Public Sub furtherMethod()
End Sub
End Class