I want to store the below data structure in the XML. I am a beginner in VB .net and XML any help would be appreciated
This is an extension of this Question enter link description here
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
I want to provide Hard-Coded Data to Serializer so that it can write data to an XML file in my desired location. I want something like this. But this is not working
Dim newPulseData As New PulserMeasDataPA()
Dim listofVpaData As List(Of TestData)
listofVpaData = New List(Of TestData) From
{
New TestData(1, New MeasureValues(1, "2", "3", 40, 1000, 100)),
New TestData(2, New MeasureValues(1, "2", "3", 40, 1000, 100))
}
newPulseData.TestData= listofVpaData
XML Serializer Look like this
Const OUTPUT_FILENAME As String = "c:\temp\test1.xml"
Dim writer As XmlWriter = XmlWriter.Create(OUTPUT_FILENAME)
Dim serializer As XmlSerializer = New XmlSerializer(GetType(PulserMeasDataPA))
serializer.Serialize(writer, newPulseData)
I want to Generate XML Like this from Above Data Structure
<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>
The Above XML is what I want to save from that data structure. I want to give values to the lists which are in these classes and I want to store that hard-coded values to the new file that it will generate. in this case, some nested lists and classes are used and I am confused about how to give values to these classes which I tried above but failed.
The reason the code you posted doesn't work, is because you haven't created a constructor for each class that accepts parameters.
Class:
TestDataHere's a simple definition for the class:
In the above code, since no constructors were defined, the default constructor is implied. If one were to explicitly include the default constructor, we'd have:
If we were to add a property to the class, and wanted to set the value using using a constructor, we could do the following:
Note: When one defines a constructor, the default constructor is no longer implicitly defined. Therefore, if it's not explicitly defined, as shown above, it won't be available. For XML serialization, a default constructor always needs to be defined.
Usage:
This creates numerous options to set the value of PortNumber. I'll cover the following:
Option 1 - Using the desired Property
Option 2 - Using an Object initializer
Note: When using
With, we use.PortNumberinstead ofPortNumber.Option 3 - Using a constructor
Option 4 - Using a method (Sub)
For this option, it's necessary to add a method (Sub) to class "TestData".
Then we can do the following: 'create new instance Dim td as TestData = New TestData()
Now, let's look at how to use a nested class.
Create class
MeasureValueInfoNote: I've changed the name of the class from
MeasureValuestoMeasureValueInfoto make it easier to understand.MeasureValueInfo:
Let's add a List of
MeasureValueInfoto classTestData.TestData:
Let's add a constructor which allows us to set the value of PortNumber and add also add MeasureValueInfo to our List.
So, now we'll have:
Once again we have multiple ways of setting the property values. I'll demonstrate the following:
Usage:
Option 1 - Using the desired Property
Option 2a - Using an Object initializer along with a constructor
Option 2b - Using an Object initializer along with a constructor
In order to use the classes with XML serialization,
Add the following Imports statements:
Imports System.XmlImports System.Xml.SerializationThen modify the classes so they look like the following:
Note: The code for the classes was adapted from the OP which was given as an answer in this post. The code was changed from using fields to using properties, initialization was added for the Lists, and constructors were added.
Resources: