使用嵌套命名空间反序列化 XML 文件时出现问题

问题描述 投票:0回答:1

我有以下 XML 文件(摘录):

<AutoindexBundle xmlns:i="...www.w3.org/2001/XMLSchema-instance" xmlns="...dev.mysite.com/settings/settingsExchange">
    <Autoindex xmlns:d2p1="...dev.mysite.com/settings/workflows/autoindex">
        <Header xmlns="...//dev.mysite.com/settings/bpsprocesses/common">
            <Guid xmlns="...//dev.mysite.com/settings/interop">0a3b7b31-907a-40bf-b55f-747046dc7ea2</Guid>
            <ID xmlns="...//dev.mysite.com/settings/interop">413</ID>
            <Name xmlns="...//dev.mysite.com/settings/interop">SD_CLEAR_STATUS</Name>
            <Description>Löscht die Einträge im Statusfeld</Description>
        </Header>
        <d2p1:AIMatchOptionSettings>
            <d2p1:MatchCodes>
...

例如,我想反序列化此文件以获得“Header.Name”下的信息。我已经在互联网上查看了许多示例,但不知何故它们并不全部与我的源文件匹配。

我首先使用“插入特殊”功能在 VB.NET 中创建一个类,并将其保存在名为“AutoindesBundle”的模块中。以下是摘录:

Public Class AutoindexBundle
    ' HINWEIS: Für den generierten Code ist möglicherweise mindestens .NET Framework 4.5 oder .NET Core/Standard 2.0 erforderlich.
    '''<remarks/>
    <System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="...//dev.mysite.com/settings/settingsExchange"),
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="...//dev.mysite.com/settings/settingsExchange", IsNullable:=False)>
    Partial Public Class AutoindexBundle
        Private autoindexField As AutoindexBundleAutoindex
        Private guidField As String
        Private ruleField As Object
        Private scheduleSettingsField As AutoindexBundleScheduleSettings
        '''<remarks/>
        Public Property Autoindex() As AutoindexBundleAutoindex
            Get
                Return Me.autoindexField
            End Get
            Set
                Me.autoindexField = Value
            End Set
        End Property
        '''<remarks/>
        Public Property Guid() As String
            Get
                Return Me.guidField
            End Get
            Set
                Me.guidField = Value
            End Set
        End Property
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute(IsNullable:=True)>
        Public Property Rule() As Object
            Get
                Return Me.ruleField
            End Get
            Set
                Me.ruleField = Value
            End Set
        End Property
        '''<remarks/>
        Public Property ScheduleSettings() As AutoindexBundleScheduleSettings
            Get
                Return Me.scheduleSettingsField
            End Get
            Set
                Me.scheduleSettingsField = Value
            End Set
        End Property
    End Class
    '''<remarks/>
    <System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="...dev.mysite.com/settings/settingsExchange")>
    Partial Public Class AutoindexBundleAutoindex
        Private headerField As Header
        Private aIMatchOptionSettingsField As AIMatchOptionSettings
        Private extDBActionsField As ExtDBActions
        Private extDBSourceField As ExtDBSource
        Private externalDataSourceTypeField As String
        Private fCActionsField As FCActions
        Private fCSourceField As FCSource
        Private iteratedRecordsAreInField As String
        Private ruleGuidField As String
        Private versionField As String
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...dev.mysite.com/settings/bpsprocesses/common")>
        Public Property Header() As Header
            Get
                Return Me.headerField
            End Get
            Set
                Me.headerField = Value
            End Set
        End Property
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...dev.mysite.com/settings/workflows/autoindex")>
        Public Property AIMatchOptionSettings() As AIMatchOptionSettings
            Get
                Return Me.aIMatchOptionSettingsField
            End Get
            Set
                Me.aIMatchOptionSettingsField = Value
            End Set
        End Property
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...dev.mysite.com/settings/workflows/autoindex")>
        Public Property ExtDBActions() As ExtDBActions
            Get
                Return Me.extDBActionsField
            End Get
            Set
                Me.extDBActionsField = Value
            End Set
        End Property
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...//dev.mysite.com/settings/workflows/autoindex")>
        Public Property ExtDBSource() As ExtDBSource
            Get
                Return Me.extDBSourceField
            End Get
            Set
                Me.extDBSourceField = Value
            End Set
        End Property
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...//dev.mysite.com/settings/workflows/autoindex")>
        Public Property ExternalDataSourceType() As String
            Get
                Return Me.externalDataSourceTypeField
            End Get
            Set
                Me.externalDataSourceTypeField = Value
            End Set
        End Property
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...//dev.mysite.com/settings/workflows/autoindex")>
        Public Property FCActions() As FCActions
            Get
                Return Me.fCActionsField
            End Get
            Set
                Me.fCActionsField = Value
            End Set
        End Property
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...//dev.mysite.com/settings/workflows/autoindex")>
        Public Property FCSource() As FCSource
            Get
                Return Me.fCSourceField
            End Get
            Set
                Me.fCSourceField = Value
            End Set
        End Property
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...//dev.mysite.com/settings/workflows/autoindex")>
        Public Property IteratedRecordsAreIn() As String
            Get
                Return Me.iteratedRecordsAreInField
            End Get
            Set
                Me.iteratedRecordsAreInField = Value
            End Set
        End Property
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...//dev.mysite.com/settings/workflows/autoindex")>
        Public Property RuleGuid() As String
            Get
                Return Me.ruleGuidField
            End Get
            Set
                Me.ruleGuidField = Value
            End Set
        End Property
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...dev.mysite.com/settings/workflows/autoindex")>
        Public Property Version() As String
            Get
                Return Me.versionField
            End Get
            Set
                Me.versionField = Value
            End Set
        End Property
    End Class
    '''<remarks/>
    <System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://dev.mysite.com/settings/bpsprocesses/common"),
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="...//dev.mysite.com/settings/bpsprocesses/common", IsNullable:=False)>
    Partial Public Class Header
        Private guidField As String
        Private idField As UShort
        Private nameField As String
        Private descriptionField As String
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...dev.mysite.com/settings/interop")>
        Public Property Guid() As String
            Get
                Return Me.guidField
            End Get
            Set
                Me.guidField = Value
            End Set
        End Property
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...//dev.mysite.com/settings/interop")>
        Public Property ID() As UShort
            Get
                Return Me.idField
            End Get
            Set
                Me.idField = Value
            End Set
        End Property
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute([Namespace]:="...//dev.mysite.com/settings/interop")>
        Public Property Name() As String
            Get
                Return Me.nameField
            End Get
            Set
                Me.nameField = Value
            End Set
        End Property
        '''<remarks/>
        Public Property Description() As String
            Get
                Return Me.descriptionField
            End Get
            Set
                Me.descriptionField = Value
            End Set
        End Property
    End Class
    '''<remarks/>
.
.
.

然后我尝试从 Header 元素中读取信息:

Private Sub DeserializeObject(ByVal filename As String)
        Console.WriteLine("Reading with XmlReader")
        Dim serializer As New XmlSerializer(GetType(AutoindexBundle.Header))
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim reader As XmlReader = XmlReader.Create(fs)
        Dim test As AutoindexBundle.Header
        test = CType(serializer.Deserialize(reader), AutoindexBundle.Header)
        fs.Close()
        Console.Write(test.Guid & ControlChars.Tab &
                      test.ID & ControlChars.Tab &
                      test.Name & ControlChars.Tab &
                      test.Description & ControlChars.Tab)
    End Sub

但不幸的是,我在 test = CType(.... 行中收到以下错误消息: System.InvalidOperationException:“XML 文档 (1, 40) 中存在错误。” 内部 Ausnahme 1: InvalidOperationException:不是预期的。

我在这里做错了什么或者我必须改变什么才能使这项工作正常进行?

xml vb.net xml-namespaces
1个回答
0
投票

这是原始文件的缩短和中和版本:

<AutoindexBundle xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://dev.mysite.com/settings/settingsExchange">
    <Autoindex xmlns:d2p1="http://dev.mysite.com/settings/workflows/autoindex">
        <Header xmlns="http://dev.mysite.com/settings/bpsprocesses/common">
            <Guid xmlns="http://dev.mysite.com/settings/interop">978094cb-6cb5-4782-afdd-bf4cd11cf4ba</Guid>
            <ID xmlns="http://dev.mysite.com/settings/interop">469</ID>
            <Name xmlns="http://dev.mysite.com/settings/interop">X_SD_CLEAR_DEL</Name>
            <Description>Löscht die Einträge im Statusfeld</Description>
        </Header>
        <d2p1:AIMatchOptionSettings>
            <d2p1:MatchCodes/>
            <d2p1:MultiMatchMode>NoMatch</d2p1:MultiMatchMode>
        </d2p1:AIMatchOptionSettings>
        <d2p1:ExtDBActions>
            <d2p1:MatchActions>
                <d2p1:DeleteRow>false</d2p1:DeleteRow>
                <d2p1:Fields/>
            </d2p1:MatchActions>
            <d2p1:NoMatchActions>
                <d2p1:Fields/>
                <d2p1:InsertRow>false</d2p1:InsertRow>
            </d2p1:NoMatchActions>
        </d2p1:ExtDBActions>
        <d2p1:ExtDBSource>
            <d2p1:AIDataSource i:type="d2p1:FixedEntriesDataSource">
                <d2p1:DSGuid>00000000-0000-0000-0000-000000000000</d2p1:DSGuid>
                <d2p1:SourceName i:nil="true"/>
            </d2p1:AIDataSource>
            <d2p1:DSFields/>
            <d2p1:FilterOption>AllDocuments</d2p1:FilterOption>
            <d2p1:UsedFilter xmlns:d4p1="http://dev.mysite.com/settings/filter">
                <d4p1:Condition i:nil="true"/>
                <d4p1:Logic>And</d4p1:Logic>
            </d2p1:UsedFilter>
        </d2p1:ExtDBSource>
        <d2p1:ExternalDataSourceType>FixedEntries</d2p1:ExternalDataSourceType>
        <d2p1:FCActions>
            <d2p1:MatchActions>
                <d2p1:DeleteRow>false</d2p1:DeleteRow>
                <d2p1:Fields>
                    <d2p1:IndexField i:type="d2p1:IndexDeletion">
                        <d2p1:Field>DEL</d2p1:Field>
                        <d2p1:Value i:nil="true"/>
                    </d2p1:IndexField>
                </d2p1:Fields>
            </d2p1:MatchActions>
            <d2p1:NoMatchActions>
                <d2p1:Fields/>
                <d2p1:InsertRow>false</d2p1:InsertRow>
            </d2p1:NoMatchActions>
        </d2p1:FCActions>
        <d2p1:FCSource>
            <d2p1:AIDataSource i:type="d2p1:FileCabinetDataSource">
                <d2p1:DSGuid>0c3b26ac-305e-48c5-952b-6aee1518cd50</d2p1:DSGuid>
                <d2p1:SourceName>Stammdaten</d2p1:SourceName>
                <d2p1:UserLogin>
                    <d2p1:ComputerAddress i:nil="true"/>
                    <d2p1:IsFixedLogin>true</d2p1:IsFixedLogin>
                    <d2p1:IsLoggedInOtherSystem>false</d2p1:IsLoggedInOtherSystem>
                    <d2p1:Organization>myBusiness</d2p1:Organization>
                    <d2p1:Password i:nil="true"/>
                    <d2p1:Port>0</d2p1:Port>
                    <d2p1:ReadOnly>false</d2p1:ReadOnly>
                    <d2p1:Token i:nil="true"/>
                    <d2p1:User>Mustermann</d2p1:User>
                    <d2p1:UserGuid>3e90584f-ec45-4a94-b6dc-1216e3b3143e</d2p1:UserGuid>
                </d2p1:UserLogin>
            </d2p1:AIDataSource>
            <d2p1:DSFields>
                <d2p1:DataSourceField>
                    <d2p1:DisplayName>ADRESSNUMMER</d2p1:DisplayName>
                    <d2p1:InternalType>Normal</d2p1:InternalType>
                    <d2p1:IsReadonly>false</d2p1:IsReadonly>
                    <d2p1:Length>-1</d2p1:Length>
                    <d2p1:Name>ADRESSNUMMER</d2p1:Name>
                    <d2p1:Type>Text</d2p1:Type>
                </d2p1:DataSourceField>
                <d2p1:DataSourceField>
                    <d2p1:DisplayName>NAME_1</d2p1:DisplayName>
                    <d2p1:InternalType>Normal</d2p1:InternalType>
                    <d2p1:IsReadonly>false</d2p1:IsReadonly>
                    <d2p1:Length>-1</d2p1:Length>
                    <d2p1:Name>NAME_1</d2p1:Name>
                    <d2p1:Type>Text</d2p1:Type>
                </d2p1:DataSourceField>
                <d2p1:DataSourceField>
                    <d2p1:DisplayName>NAME_2</d2p1:DisplayName>
                    <d2p1:InternalType>Normal</d2p1:InternalType>
                    <d2p1:IsReadonly>false</d2p1:IsReadonly>
                    <d2p1:Length>-1</d2p1:Length>
                    <d2p1:Name>NAME_2</d2p1:Name>
                    <d2p1:Type>Text</d2p1:Type>
                </d2p1:DataSourceField>
                <d2p1:DataSourceField>
                    <d2p1:DisplayName>NAME_3</d2p1:DisplayName>
                    <d2p1:InternalType>Normal</d2p1:InternalType>
                    <d2p1:IsReadonly>false</d2p1:IsReadonly>
                    <d2p1:Length>-1</d2p1:Length>
                    <d2p1:Name>NAME_3</d2p1:Name>
                    <d2p1:Type>Text</d2p1:Type>
                </d2p1:DataSourceField>
                <d2p1:DataSourceField>
                    <d2p1:DisplayName>NAME_4</d2p1:DisplayName>
                    <d2p1:InternalType>Normal</d2p1:InternalType>
                    <d2p1:IsReadonly>false</d2p1:IsReadonly>
                    <d2p1:Length>-1</d2p1:Length>
                    <d2p1:Name>NAME_4</d2p1:Name>
                    <d2p1:Type>Text</d2p1:Type>
                </d2p1:DataSourceField>
                <d2p1:DataSourceField>
                    <d2p1:DisplayName>MATCHCODE</d2p1:DisplayName>
                    <d2p1:InternalType>Normal</d2p1:InternalType>
                    <d2p1:IsReadonly>false</d2p1:IsReadonly>
                    <d2p1:Length>-1</d2p1:Length>
                    <d2p1:Name>MATCHCODE</d2p1:Name>
                    <d2p1:Type>Text</d2p1:Type>
                </d2p1:DataSourceField>
            </d2p1:DSFields>
            <d2p1:FilterOption>Filter</d2p1:FilterOption>
            <d2p1:UsedFilter xmlns:d4p1="http://dev.mysite.com/settings/filter">
                <d4p1:Condition i:type="d4p1:ExtendedCondition">
                    <d4p1:ExtendedQuery xmlns:d6p1="http://dev.mysite.com/settings/web/querybuilder">
                        <d6p1:Root>
                            <d6p1:Conditions>
                                <d6p1:BaseCondition i:type="d6p1:Condition">
                                    <d6p1:DBName>DEL</d6p1:DBName>
                                    <d6p1:Provider i:type="d6p1:EmptyConditionProvider">
                                        <d6p1:Operation>NotEmpty</d6p1:Operation>
                                    </d6p1:Provider>
                                </d6p1:BaseCondition>
                            </d6p1:Conditions>
                            <d6p1:Operation>Or</d6p1:Operation>
                        </d6p1:Root>
                    </d4p1:ExtendedQuery>
                </d4p1:Condition>
                <d4p1:Logic>And</d4p1:Logic>
            </d2p1:UsedFilter>
        </d2p1:FCSource>
        <d2p1:IteratedRecordsAreIn>FileCabinet</d2p1:IteratedRecordsAreIn>
        <d2p1:RuleGuid>00000000-0000-0000-0000-000000000000</d2p1:RuleGuid>
        <d2p1:Version>5.1.0.1</d2p1:Version>
    </Autoindex>
    <Guid>978094cb-6cb5-4782-afdd-bf4cd11cf4ba</Guid>
    <Rule xmlns:d2p1="http://dev.mysite.com/settings/notification" i:nil="true"/>
    <ScheduleSettings xmlns:d2p1="http://dev.mysite.com/settings/common/dwschedule" i:nil="true"/>
</AutoindexBundle>```
© www.soinside.com 2019 - 2024. All rights reserved.