VB.net 中 xml 文件的序列化

serialization of xml file in VB.net

我有以下数据结构

Public Class MeasData
Public Number As Integer
Public Amp As Single
Public Fall As Single
Public Width As Single
Public setPRF As Integer
Public Volt As Single
Public setWidth As Single

序列化器Class代码

Public Class TestDataSerializer
Public PulsTestData As New List(Of PulserMeasData)
Private PulsTestDataSorted As New SortedList(Of Integer, PulserMeasData)

Public Sub AddPulsTestData(ByVal ChaNum As Integer,
                           ByVal PAmp As Single,
                            ByVal PFallTime As Single,
                            ByVal PWidth As Single,
                            ByVal setPrf As Integer,
                            ByVal setVoltage As Single,
                            ByVal setPulswidth As Single)

    Dim Data As New PulserMeasData(ChaNum, PAmp, PFallTime, PWidth, setPrf, setVoltage, setPulswidth)

    If PulsTestDataSorted.ContainsKey(ChaNum) Then
        PulsTestDataSorted(ChaNum) = Data
    Else
        PulsTestDataSorted.Add(ChaNum, Data)
    End If
    PulsTestData = PulsTestDataSorted.Values.ToList ' XML Serializer can not handle SortedLists
    Dim serializer As New XMLSerializer(GetType(List(Of PulserMeasData)))
    Using file As System.IO.FileStream = System.IO.File.Open("obj.xml", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
        serializer.Serialize(file, PulsTestData)
    End Using
End Sub

结束Class

我想从上面的数据结构生成 XML 具有不同的设置宽度 prf 和电压值

尝试以下操作:

Imports System.Xml
Imports System.Xml.Serialization
Module Module1
    Const INPUT_FILENAME As String = "c:\temp\test.xml"
    Const OUTPUT_FILENAME As String = "c:\temp\test1.xml"
    Sub Main()
        'read data
        Dim reader As XmlReader = XmlReader.Create(INPUT_FILENAME)
        Dim serializer As XmlSerializer = New XmlSerializer(GetType(PulserMeasDataPA))
        Dim PulserMeasDataPA As PulserMeasDataPA = serializer.Deserialize(reader)

        Dim writer As XmlWriter = XmlWriter.Create(OUTPUT_FILENAME)

        serializer.Serialize(writer, PulserMeasDataPA)

        Dim PulsTestData As New List(Of PulserMeasData)
        Dim PulsTestDataSorted As New SortedList(Of Integer, PulserMeasData)

        Dim newPulseData As New PulserMeasDataPA()
        Dim newListData As New List(Of TestData)
        newPulseData.TestData = newListData



        For Each PD In PulsTestDataSorted.GroupBy(Function(x) x.Key)
            Dim newTestData As New TestData()
            newListData.Add(newTestData)
            newTestData.PortNumber = PD.Key

            newTestData.MeasureValues = New List(Of MeasureValues)

            For Each testData In PD
                Dim newValues As New MeasureValues
                newTestData.MeasureValues.Add(newValues)

                newValues.Amplitude = testData.Value.Amplitude

            Next
        Next

    End Sub

End Module

Public Class PulserMeasData
    Public ChannelNumber As Integer
    Public Amplitude As Single
    Public Fall As Single
    Public Width As Single
    Public setPRF As Integer
    Public setVoltage As Single
    Public setWidth As Single
End Class

Public Class PulserMeasDataPA
    <XmlElement()>
    Public TestData As List(Of TestData)
End Class
Public Class TestData
    Public PortNumber As Integer
    <XmlArray("Measurement")>
    <XmlArrayItem("MeasureValues")>
    Public MeasureValues As List(Of MeasureValues)
End Class
Public Class MeasureValues
    Public Amplitude As String
    Public Fall As String
    Public Width As String
    Public setWidth As Decimal
    Public PRF As Decimal
    Public Volt As Decimal
End Class

这是我使用的XML文件

<?xml version="1.0" encoding="utf-8" ?>
<PulserMeasDataPA>
  <TestData>
    <PortNumber>1</PortNumber>
    <Measurement>
      <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>
          40</setWidth>
          <PRF>
            1000</PRF>
            <Volt>
              100</Volt>
            </MeasureValues>
      <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>
          100</setWidth>
          <PRF>
            1000</PRF>
            <Volt>
              100</Volt>
            </MeasureValues>
      <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>
          40</setWidth>
          <PRF>
            5000</PRF>
            <Volt>
              50</Volt>
            </MeasureValues>
      <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>
          100</setWidth>
          <PRF>
            5000</PRF>
            <Volt>
              50</Volt>
            </MeasureValues>
    </Measurement>
  </TestData>
  <TestData>
    <PortNumber>2</PortNumber>
    <Measurement>
      <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>
          40</setWidth>
          <PRF>
            1000</PRF>
            <Volt>
              100</Volt>
            </MeasureValues>
      <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>
          100</setWidth>
          <PRF>
            1000</PRF>
            <Volt>
              100</Volt>
            </MeasureValues>
      <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>
          40</setWidth>
          <PRF>
            5000</PRF>
            <Volt>
              50</Volt>
            </MeasureValues>
      <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>
          100</setWidth>
          <PRF>
            5000</PRF>
            <Volt>
              50</Volt>
            </MeasureValues>
    </Measurement>
  </TestData>
</PulserMeasDataPA>