无法在.Net中使用XmlSerializer反序列化抽象类数组

时间:2014-01-30 19:30:59

标签: xml vb.net xml-parsing xsd deserialization

我正在尝试从我给出的XML文件创建对象模型。我获取格式的.xsd并使用xsd.exe生成用于对象模型的类。我使用以下代码来运行反序列化:

Dim serializer As New XmlSerializer(GetType(tDefinitions))
Dim xmlStream As New StreamReader("c:\bpmn\bpmn.xml", System.Text.Encoding.UTF8)
Dim document As tDefinitions = CType(serializer.Deserialize(xmlStream), tDefinitions)

tDefinitions的定义如下:

<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038"), _
 System.SerializableAttribute(), _
 System.Diagnostics.DebuggerStepThroughAttribute(), _
 System.ComponentModel.DesignerCategoryAttribute("code"), _
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://www.omg.org/spec/BPMN/20100524/MODEL"), _
 System.Xml.Serialization.XmlRootAttribute("definitions", [Namespace]:="http://www.omg.org/spec/BPMN/20100524/MODEL", IsNullable:=False)> _
Partial Public Class tDefinitions

    Private importField() As tImport

    Private extensionField() As tExtension

    Private rootElementField() As tRootElement

    Private bPMNDiagramField() As BPMNDiagram

'... removed code for brevity

    <System.Xml.Serialization.XmlElementAttribute("rootElement")> _
    Public Property rootElement() As tRootElement()
        Get
            Return Me.rootElementField
        End Get
        Set(value As tRootElement())
            Me.rootElementField = value
        End Set
    End Property

'... removed code for brevity

当我运行代码时,它成功创建了一个tDefinitions对象。但是,只会填充具体类型数组的字段。由于tRootElement是一个抽象类类型,因此rootElementField()最终为Nothing。如果我创建了继承tRootElement的所有特定类型的数组,那么这些数组会被填充。这是tRootElement的定义:

<System.Xml.Serialization.XmlIncludeAttribute(GetType(tSignal)), _
 System.Xml.Serialization.XmlIncludeAttribute(GetType(tResource)), _
 System.Xml.Serialization.XmlIncludeAttribute(GetType(tPartnerRole)), _
 System.Xml.Serialization.XmlIncludeAttribute(GetType(tPartnerEntity)), _
'Many more types removed for brevity
 System.Xml.Serialization.XmlIncludeAttribute(GetType(tGlobalBusinessRuleTask)), _
 System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038"), _
 System.SerializableAttribute(), _
 System.Diagnostics.DebuggerStepThroughAttribute(), _
 System.ComponentModel.DesignerCategoryAttribute("code"), _
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://www.omg.org/spec/BPMN/20100524/MODEL"), _
 System.Xml.Serialization.XmlRootAttribute("rootElement", [Namespace]:="http://www.omg.org/spec/BPMN/20100524/MODEL", IsNullable:=False)> _
Partial Public MustInherit Class tRootElement
    Inherits tBaseElement
End Class

关于我能做些什么才能使这项工作的想法?我会考虑编写自己的解析器,除了xml架构非常大,它可能需要数周的繁重工作。

0 个答案:

没有答案