1

我无法将 xml 属性反序列化为类属性。

<?xml version="1.0" encoding="UTF-8" ?> 
<Ball clientid="xyz">
    <Name>Tommy</Name>
    <ballColor transactionid="1234">White</ballColor>
    <radius>9</radius>
    <PowerLevel>9001</PowerLevel>
    <RandomProp>This is another property</RandomProp>
</Ball>

是我正在使用的 XML... 现在我将发布我拥有的代码。问题是我能够将 clientid 获取到属性中,但不能获取“transactionid”所以我无法提取子元素的属性

Imports System.Xml.Serialization
Public Class Ball
    Inherits ballColor
    Public Property Name As String
    Public Property radius As Double
    Public Property PowerLevel As String
    Public Property RandomProp As String
    <XmlAttribute("clientid")> Public Property clientid() As String


    Public Sub New()

    End Sub


End Class
<XmlRoot("Ball")>
Public Class ballColor
    <XmlElement("ballColor")> Public Property ballColor As String
    <XmlAttribute("transactionid")> Public Property transactionid As String
    Public Sub New()

    End Sub
End Class

所以我在表单上进行了实际的反序列化调用,但这似乎不是我的问题,因为当我运行它时,除了事务 ID 之外,实际上每个属性都被正确填充。我究竟做错了什么??

4

2 回答 2

3

我宁愿将 ballColor 作为球内的属性。在下面试试这个。

<Serializable>
<XmlRoot("Ball")>
Public Class Ball
    Public Property Name As String
    Public Property radius As Double
    Public Property PowerLevel As String
    Public Property RandomProp As String
    <XmlAttribute("clientid")> Public Property clientid() As String
    Public Property ballColor As ballColor

    Public Sub New()
        ballColor = New ballColor
    End Sub

End Class

<Serializable>
Public Class ballColor
    <XmlText> Public Property ballColor As String
    <XmlAttribute("transactionid")> Public Property transactionid As String

    Public Sub New()

    End Sub
End Class
于 2013-03-26T15:13:22.760 回答
1

将您的 ballColor 类更改为:

Public Class ballColor

    <XmlAttribute("transactionid")> 
    Public Property transactionid As String   

    <XmlText> 
    Public Property Value As String
End Class

这将在字段中存储属性transactionidballcolor.transactionidballcolorballcolor.value

更改主球类别

Public Class Ball
  Public Property Name As String
    Public Property radius As Double
    Public Property PowerLevel As String
    Public Property RandomProp As String
    <XmlAttribute("clientid")> Public Property clientid() As String
    Public Property _ballColor As ballColor
End Class
于 2013-03-26T14:54:57.503 回答