在VB中序列化XMLArrayItems的动态列表

时间:2016-01-12 00:30:51

标签: xml vb.net api serialization

我最难反序列化XMLArray,因为XMLArray构建得不好。遗憾的是,我无法更改服务器(API集成)的响应,因此我必须使我的对象与他们的对象相匹配。当我查询服务器时,我回到XML:

<instrument>
    <reqs>
        <party_middle_name>optional</party_middle_name>
        <party_first_name>optional</party_first_name>
        <party_suffix>optional</party_suffix>
        <party_last_name>optional</party_last_name>
        <party_marital_status>optional</party_marital_status>
    </reqs>
    <name>ABANDONMENT</name>
    <countyInstrument>true</countyInstrument>
</instrument>

他们有一半是正确的......但是party_middle_name,party_first_name等可以拥有比呈现更多的XMLArrayItems。我必须考虑随机显示或不显示的数组项。我也没有可以显示的所有可选元素的大量列表,因此我不能只编写可以为空的项目集合并定义每个项目。

到目前为止,我有:

<Serializable()> _
Public Class InstrumentList
    Private Property _name As String
    Private Property _countyInstrument As Boolean
    Private Property _reqsList As List(Of ReqsList)

    <XmlElement("name")> _
    Public Property Name() As String
        Get
            Return Me._name
        End Get
        Set(value As String)
            Me._name = value
        End Set
    End Property

    <XmlElement("countyInstrument")> _
    Public Property CountyInstrument() As Boolean
        Get
            Return Me._countyInstrument
        End Get
        Set(value As Boolean)
            Me._countyInstrument = value
        End Set
    End Property

    <XmlArray("reqs"), XmlArrayItem(" [ MISSING PIECE HERE ] ")> _
    Public Property ReqsList() As List(Of ReqsList)
        Get
            Return Me._reqsList
        End Get
        Set(value As List(Of ReqsList))
            Me._reqsList = value
        End Set
    End Property

    Public Sub New()
    End Sub
End Class


<Serializable()> _
Public Class ReqsList
    Private Property _requirement As String

    <XmlText> _
    Public Property Requirement() As String
        Get
            Return Me._requirement
        End Get
        Set(value As String)
            Me._requirement = value
        End Set
    End Property

    Public Sub New()
    End Sub
End Class

我需要根据XMLArrayItem的XML Element名称创建动态元素,然后将其与XMLText配对。任何帮助将不胜感激!!谢谢!

1 个答案:

答案 0 :(得分:0)

好的,希望,也许,这可以帮助别人。没有雄辩的方法来做到这一点,但我想出了一种方法来创建某种解决方案。

首先我们从服务器上取回Xml Stream,并将其转换为XmlDocument。然后我们选择了我要修复的所有XmlNode并创建了一个XmlNodeList。然后,我们逐步执行XmlNodeList并更正每个ChildNode。我们替换ChildNodes,并返回InnerXML的String。

然后,我将一个函数写入Parse XML Stream,并将其写入我想要的对象中。

所以我的财产看起来像:

    <XmlArray("reqs"), XmlArrayItem("req")> _
    Public Property ReqsList() As List(Of ReqItem)
        Get
            Return Me._reqsList
        End Get
        Set(value As List(Of ReqItem))
            Me._reqsList = value
        End Set
    End Property

我的FixInstrumentReqs函数如下所示:

    Public Shared Function FixInstrumentReqs(stream As String) As String
        Dim xml As New XmlDocument()
        xml.LoadXml(stream)

        Dim xmlNodes As XmlNodeList = xml.SelectNodes("/xml/ServiceResponse/List/instrument/reqs")

        For Each x As XmlElement In xmlNodes
            For i As Integer = 0 To x.ChildNodes.Count Step 1
                Dim y As XmlElement = x.ChildNodes.Item(i)
                If IsNothing(y) = False Then
                    Dim _new As XmlElement = xml.CreateElement("req")
                    Dim attr As XmlAttribute = xml.CreateAttribute("name")
                    attr.Value = y.Name
                    _new.SetAttributeNode(attr)
                    _new.InnerText = y.InnerText
                    y.ParentNode.ReplaceChild(_new, y)
                End If
            Next
        Next

        stream = xml.InnerXml.ToString()
        Return stream
    End Function

希望这有助于其他人。