反序列化元素和数组

时间:2011-11-26 03:07:04

标签: xml vb.net arrays deserialization

我遇到了将XML反序列化到我的类的问题。在将数组 det 放入类中之前,它工作正常。

我在反序列化发生后放了一个手表(data = Ctype(...)),我可以看到所有的信息都被正确加载(ide,emit,dest),除了 det 元素:加载 nItem = 1 属性的 det ,但 det nItem = 2 属性为不

使用手表,我可以看到对象像单个变量一样加载,而不是像我在属性声明中指定的向量

有什么建议吗?谢谢!

单击按钮时,下面是反序列化的位置:

Protected Sub btnDes_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDes.Click
        Dim data As New nf

        'DeSerializacao XML
        Dim Deserializer As New Serialization.XmlSerializer(GetType(nf))
        Dim DataFile As New FileStream(Server.MapPath("App_Data\pedro.xml"), FileMode.Open, FileAccess.Read, FileShare.None)

        data = CType(Deserializer.Deserialize(DataFile), nf)
        DataFile.Close()
        DataFile.Dispose()

 End Sub

以下是我的XML代码的一部分

<NF>
                <iNF>
                        <ide>
                                <cUF>31</cUF>                           
                        </ide>
                        <emit>                          
                                <xNome>Tradimaq  Ltda - MG</xNome>                              
                        </emit>
                        <dest>                          
                                <xNome>GEMAQUINAS LTDA</xNome>                          
                        </dest>
                        <det nItem="1">
                                <prod>
                                        <cProd>3.04.000000109</cProd>                                   
                                </prod>                         
                        </det>
                        <det nItem="2">
                                <prod>
                                        <cProd>3.01.580048839</cProd>                                   
                                </prod>

                        </det>

                </iNF>          
</NF>

以下是我为接收反序列化而创建的类的一部分:

Public Class NF

    Public Property iNF As infNFe

    Public Sub New()


    End Sub

End Class
------------------------------------------------------------------------
Public Class iNF


    Public Sub New()

    End Sub

    Public Property ide As ide
    Public Property emit As emit
    Public Property avulsa_info As avulsa
    Public Property dest As dest

    Public Property det() As det


End Class
------------------------------------------------------------------------
Public Class det    

    <XmlElement("prod")> Public Property prod As prod

    Public Sub New()


    End Sub

End Class


Public Class prod

    Public Property cProd As String   

    Public Sub New()

    End Sub

End Class
------------------------------------------------------------------------

Public Class ide
    Public Property cUF As String
End Class

------------------------------------------------------------------------

Public Class emit
    Public Property xNome As String
End Class

------------------------------------------------------------------------

Public Class dest
    Public Property xNome As String
End Class

------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

更改此行:

Public Property det() As det

进入这个:

<XmlElement("det")> Public Property det As det()

虽然我不确定为什么使用XmlElementAttribute会有效但省略它会失败。

请注意

Property det() as det 

不是det数组的属性,parantheses用于参数列表,在这种情况下为空。对于数组的属性,您必须将parantheses放在最后:

Property det As det()