XML <ArrayOfVPADeviceMeasureData xmlns=''> 的反序列化不是预期的

Deserializing of XML <ArrayOfVPADeviceMeasureData xmlns=''> was not expected

我正在尝试反序列化一个 XML 文件,但我收到了一个错误,我不知道它为什么会出现

错误: XML 文档 (2, 2) 中存在错误。没想到。

我有以下数据结构

Public Class VPADeviceMeasureData
<XmlElement()>
Public ChannelNumber As Integer
<XmlArray("Measurement")>
<XmlArrayItem("MeasureValues")>
Public MeasureValues As List(Of MeasureValues)
Public Sub New()
End Sub
Public Sub New(ChannelNumber As Integer, MeasureValues As List(Of MeasureValues))
    Me.ChannelNumber = ChannelNumber
    Me.MeasureValues = MeasureValues
End Sub
End Class

Public Class MeasureValues
Public Amplitude As String
Public Fall As String
Public Width As String
Public setWidth As Decimal
Public settPRF As Decimal
Public setVoltage As Decimal
Public Sub New()
End Sub
Public Sub New(ByVal PAmp As Single, ByVal PFallTime As Single, ByVal PWidth As Single, ByVal SPRF As Integer, ByVal SVoltage As Single, ByVal SWidth As Single)
    settPRF = SPRF
    setVoltage = SVoltage
    setWidth = SWidth
    Amplitude = PAmp
    Fall = PFallTime
    Width = PWidth
End Sub
End Class

我的反序列化器

Dim reader As XmlReader = XmlReader.Create(FILE1)
    Dim writer As XmlWriter = XmlWriter.Create(FILE2)
    Dim serialization As XmlSerializer = New XmlSerializer(GetType(VPADeviceMeasureData))
    Dim PulserMeasDataPA As VPADeviceMeasureData = serialization.Deserialize(reader)
    serialization.Serialize(writer, PulserMeasDataPA)

我试过了,但我丢失了所有数据,它没有正确地再次写入 XML

<XmlRoot(ElementName:="ArrayOfVPADeviceMeasureData")>

另外,我的“ArrayOfVPADeviceMeasureData”在我的数据结构中有这个,但是当我序列化我的数据时,它也将“ArrayOfVPADeviceMeasureData”保存到我的xml,我不想保存[=16] =]

这就是我最初序列化的方式。

Dim vpaDAta As New List(Of VPADeviceMeasureData) From
    {
        New VPADeviceMeasureData With {.ChannelNumber = 1, .MeasureValues = New List(Of MeasureValues) From
        {
            New MeasureValues With {.Amplitude = 1, .Fall = 120, .settPRF = 1000, .setVoltage = 50, .setWidth = 40, .Width = 500},
            New MeasureValues With {.Amplitude = 1, .Fall = 120, .settPRF = 1000, .setVoltage = 50, .setWidth = 40, .Width = 500}}},
        New VPADeviceMeasureData With {.ChannelNumber = 2, .MeasureValues = New List(Of MeasureValues) From
        {
            New MeasureValues With {.Amplitude = 1, .Fall = 120, .settPRF = 1000, .setVoltage = 50, .setWidth = 40, .Width = 500},
            New MeasureValues With {.Amplitude = 1, .Fall = 120, .settPRF = 1000, .setVoltage = 50, .setWidth = 40, .Width = 500}
    }}}

    Dim writer As XmlWriter = XmlWriter.Create(FILE1)
    Dim serialization As XmlSerializer = New XmlSerializer(GetType(List(Of VPADeviceMeasureData)))
    serialization.Serialize(writer, vpaDAta)

危险信号是您有两个不同的序列化器。您应该能够使用相同的序列化程序进行读取和写入,因此请先尝试这样做。我认为您可以使用额外的 class 来表示文件的 xml 根目录。看看我是如何让它工作的:

<XmlRoot>
Public Class ArrayOfVPADeviceMeasureData
    <XmlElement("VPADeviceMeasureData")>
    Public Property VPADeviceMeasureDatas As List(Of VPADeviceMeasureData)
End Class

Public Class VPADeviceMeasureData
    <XmlElement()>
    Public ChannelNumber As Integer
    <XmlArray("Measurement")>
    <XmlArrayItem("MeasureValues")>
    Public MeasureValues As List(Of MeasureValues)
    Public Sub New()
    End Sub
    Public Sub New(ChannelNumber As Integer, MeasureValues As List(Of MeasureValues))
        Me.ChannelNumber = ChannelNumber
        Me.MeasureValues = MeasureValues
    End Sub
End Class

Public Class MeasureValues
    Public Amplitude As String
    Public Fall As String
    Public Width As String
    Public setWidth As Decimal
    Public settPRF As Decimal
    Public setVoltage As Decimal
    Public Sub New()
    End Sub
    Public Sub New(ByVal PAmp As Single, ByVal PFallTime As Single, ByVal PWidth As Single, ByVal SPRF As Integer, ByVal SVoltage As Single, ByVal SWidth As Single)
        settPRF = SPRF
        setVoltage = SVoltage
        setWidth = SWidth
        Amplitude = PAmp
        Fall = PFallTime
        Width = PWidth
    End Sub
End Class

并使用单个序列化程序对其进行测试。你应该在序列化器中使用相同的class进行读取和写入,无论序列化器是否相同。

Dim FILE1 = "myFile.xml"

Dim root As New ArrayOfVPADeviceMeasureData()

Dim vpaDAta As New List(Of VPADeviceMeasureData) From
{
    New VPADeviceMeasureData With {.ChannelNumber = 1, .MeasureValues = New List(Of MeasureValues) From
    {
        New MeasureValues With {.Amplitude = 1, .Fall = 120, .settPRF = 1000, .setVoltage = 50, .setWidth = 40, .Width = 500},
        New MeasureValues With {.Amplitude = 1, .Fall = 120, .settPRF = 1000, .setVoltage = 50, .setWidth = 40, .Width = 500}}},
    New VPADeviceMeasureData With {.ChannelNumber = 2, .MeasureValues = New List(Of MeasureValues) From
    {
        New MeasureValues With {.Amplitude = 1, .Fall = 120, .settPRF = 1000, .setVoltage = 50, .setWidth = 40, .Width = 500},
        New MeasureValues With {.Amplitude = 1, .Fall = 120, .settPRF = 1000, .setVoltage = 50, .setWidth = 40, .Width = 500}
}}}

root.VPADeviceMeasureDatas = vpaDAta

Dim serializer As New XmlSerializer(GetType(ArrayOfVPADeviceMeasureData))

Using writer = New StreamWriter(FILE1, False)
    serializer.Serialize(writer, root)
End Using

Using reader = New StreamReader(FILE1)
    root = serializer.Deserialize(reader)
End Using