1

I have Following data structure

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

Serializer Class Code

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

End Class

I Want to Generate XML from the above Data Strucuture with different values of set width prf and voltage

2
  • Interesting that this exact question was posted yesterday under a different user name. The post contained some comments that included a link to some code, which could have been adapted for the XML serialization. Commented Feb 9, 2022 at 19:36
  • @Eric Tuco have a look at this link OR search convert txt to xml code analysistabs.com/vba/code-convert-excel-to-xml Commented Feb 13, 2022 at 22:11

1 Answer 1

3

Try following :

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

Here is the XML file I used

<?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>
Sign up to request clarification or add additional context in comments.

20 Comments

can you please tell me where is is implementing the logic of setWidth which I have mentioned above? i.e. writing all the width values with prf and volt and when width values is completed once in next data set it will update the prf and volt ... can you please explain little bit?
Run the code and the put break point after the variable PulserMeasDataPA is loaded. You will see all the classes and properties.
Dim serializer As XMLSerializer = New XMLSerializer(GetType(PulserMeasDataPA)) Getting Error at this line : Type Expected BC30182.
The type is the classes below the module. You should not be getting this error.
Yes i can see from the code PulserMeasDataPA is passed as type but still i am getting this Error :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.