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

Time:02-18

I'm trying to deserialize an XML file but i am getting the error which I don't know why it is coming

Error : There is an error in XML document (2, 2). was not expected.

I have the following Data Structure

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

My DeSerializer

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)

This is my XML

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfVPADeviceMeasureData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<VPADeviceMeasureData>
    <ChannelNumber>1</ChannelNumber>
    <Measurement>
        <MeasureValues>
            <Amplitude>1</Amplitude>
            <Fall>120</Fall>
            <Width>500</Width>
            <setWidth>40</setWidth>
            <settPRF>1000</settPRF>
            <setVoltage>50</setVoltage>
        </MeasureValues>
        <MeasureValues>
            <Amplitude>1</Amplitude>
            <Fall>120</Fall>
            <Width>500</Width>
            <setWidth>40</setWidth>
            <settPRF>1000</settPRF>
            <setVoltage>50</setVoltage>
        </MeasureValues>
    </Measurement>
</VPADeviceMeasureData>
<VPADeviceMeasureData>
    <ChannelNumber>2</ChannelNumber>
    <Measurement>
        <MeasureValues>
            <Amplitude>1</Amplitude>
            <Fall>120</Fall>
            <Width>500</Width>
            <setWidth>40</setWidth>
            <settPRF>1000</settPRF>
            <setVoltage>50</setVoltage>
        </MeasureValues>
        <MeasureValues>
            <Amplitude>1</Amplitude>
            <Fall>120</Fall>
            <Width>500</Width>
            <setWidth>40</setWidth>
            <settPRF>1000</settPRF>
            <setVoltage>50</setVoltage>
        </MeasureValues>
    </Measurement>
</VPADeviceMeasureData>
</ArrayOfVPADeviceMeasureData>

I tried with this but I am missing all the data it is not correctly writing it again to XML

<XmlRoot(ElementName:="ArrayOfVPADeviceMeasureData")>

Plus i "ArrayOfVPADeviceMeasureData" have this in my Data Structure but when I Serialize it my data it is saving "ArrayOfVPADeviceMeasureData" this as well to my xml which I don't want to save

this is how I am serializing in the first place.

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)

CodePudding user response:

The red flag is you have two different serializers. You should be able to read and write using the same serializer so start by trying to do that. I think you could use an additional class to represent the xml root of the file. See how I got it working:

<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

And use a single serializer to test it. You should use the same class in the serializer for reading and writing, whether the serializer is actually the same or not.

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
  • Related