Mile M. Mile M. - 6 months ago 18
Vb.net Question

VB.NET Repeat elements in XML with different text

I'm making in VB.NET app, my Form have textboxes: txtName, txtLName, txtAge.

I need to enter multiple times this 3 variables(Name,LName,Age) and make XML file.

I use this code:

Dim settings As New XmlWriterSettings()
settings.Indent = True

' Initialize the XmlWriter.
Dim XmlWrt As XmlWriter = XmlWriter.Create("Test1.xml", settings)

With XmlWrt

' Write the Xml declaration.
.WriteStartDocument()


' Write the root element.
.WriteStartElement("Invoice")

' Start our first person.
.WriteStartElement("Person")

' The person nodes.

.WriteStartElement("Name")
.WriteString(txtName.Text.ToString())
.WriteEndElement()

.WriteStartElement("LName")
.WriteString(txtLName.Text.ToString())
.WriteEndElement()

.WriteStartElement("Age")
.WriteString(txtAge.Text.ToString())
.WriteEndElement()

' The end of this person.
.WriteEndElement()

' Close the XmlTextWriter.
.WriteEndDocument()
.Close()

End With

MessageBox.Show("XML file saved.")


But with this code I only got XML for one person.

I need one XML with more people. For example like this I need:

<?xml version="1.0" encoding="utf-8"?>
<Invoice>
<Person>
<Name>Pero</Name>
<LName>Zder</LName>
<Age>23</Age>
</Person>
<Person>
<Name>Pero</Name>
<LName>Zder</LName>
<Age>23</Age>
</Person>
<Person>
<Name>Pero</Name>
<LName>Zder</LName>
<Age>23</Age>
</Person>
</Invoice>


edit: More info how app should work:
I have 3 textboxes (txtName, txtLName, txtAge) and when I fill textboxes and click Next, after click textboxes cleared and I start filling new person. When I enter 20 persons I want to click export to XML, and got all 20 persons in XML file. Number of entries is changeable

Answer

Personally I would use serialization for this like so.....

Imports System.IO
Imports System.Xml.Serialization

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim invoice As New Invoice

    ' replace this with the text from your textboxes on your form
    invoice.Person.Add(New Person() With {.Age = 29, .LName = "Doe", .Name = "John"})
    invoice.Person.Add(New Person() With {.Age = 25, .LName = "Doe", .Name = "Jane"})
    invoice.Person.Add(New Person() With {.Age = 55, .LName = "Doe", .Name = "Fred"})
    invoice.Person.Add(New Person() With {.Age = 75, .LName = "Doe", .Name = "Harry"})

    'Serialize object to a text file.
    Dim objStreamWriter As New StreamWriter("Invoice.xml") ' in the build folder
    Dim x As New XmlSerializer(invoice.GetType)

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' simply add these two lines to remove the Namespaces by creating and setting XmlSerializerNamespaces to empty strings
    Dim ns = New XmlSerializerNamespaces()
    ns.Add("", "")
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' then add 'ns' the Serialize function
    x.Serialize(objStreamWriter, invoice, ns)
    objStreamWriter.Close()


    'Deserialize text file to a new object.
    Dim objStreamReader As New StreamReader("Invoice.xml") ' in the build folder
    Dim DeserializeObj As New Invoice()
    ' DeserializeObj will contain your objects Deserialized from the Invoice.xml file
    DeserializeObj = x.Deserialize(objStreamReader)
    objStreamReader.Close()

    Serialize(Of Invoice)(invoice)

End Sub


End Class

<XmlRoot(ElementName:="Person")>
Public Class Person
    <XmlElement(ElementName:="Name")>
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
    <XmlElement(ElementName:="LName")>
    Public Property LName() As String
        Get
            Return m_LName
        End Get
        Set
            m_LName = Value
        End Set
    End Property
    Private m_LName As String
    <XmlElement(ElementName:="Age")>
    Public Property Age() As String
        Get
            Return m_Age
        End Get
        Set
            m_Age = Value
        End Set
    End Property
    Private m_Age As String
End Class

<XmlRoot(ElementName:="Invoice")>
Public Class Invoice

    Sub New()
        Me.Person = New List(Of Person)
    End Sub
    <XmlElement(ElementName:="Person")>
    Public Property Person() As List(Of Person)
        Get
            Return m_Person
        End Get
        Set
            m_Person = Value
        End Set
    End Property
    Private m_Person As List(Of Person)
End Class
Comments