VB.NET 使用不同的文本重复 XML 中的元素

VB.NET Repeat elements in XML with different text

我在 VB.NET 应用程序中制作,我的表单有文本框:txtName、txtLName、txtAge。

我需要多次输入这 3 个变量(姓名、姓名、年龄)并制作 XML 文件。

我使用这个代码:

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.")

但是用这个代码我只得到了一个人的XML。

我需要一个 XML 和更多人一起。例如像这样我需要:

<?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>

编辑:更多信息应用应该如何工作: 我有 3 个文本框(txtName、txtLName、txtAge),当我填写文本框并单击“下一步”时,单击文本框后清除并开始填写新人。当我输入 20 个人时,我想单击导出到 XML,并在 XML 文件中获得所有 20 个人。条目数可变

编辑:

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

编辑:我来自 APP 的代码:

Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Imports XML_PisacTest

Public Class Form1

    Dim faktura As New Faktura

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        faktura.Ustanova.Add(New Ustanova() With {.Age = txtIsp.Text, .LName = txtFil.Text, .Name = txtName.Text})
        txtName.Text = ""
        txtLName.Text = ""
        txtAge.Text = ""

    End Sub


    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click


        Dim ns As New XmlSerializerNamespaces()
        ns.Add("", "")


        Dim objStreamWriter As New StreamWriter("Invoice.xml") ' in the build folder
        Dim x As New XmlSerializer(faktura.GetType)
        x.Serialize(objStreamWriter, faktura, ns)
        objStreamWriter.Close()

        ' 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()
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        faktura.Osiguranik.Add(New Osiguranik() With {.Fil = txtFil.Text, .Isp = txtIsp.Text, .Prez = txtPrez.Text})
        txtFil.Text = ""
        txtIsp.Text = ""
        txtPrez.Text = ""
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        faktura.DodatneDijagnoze.Add(New DodatneDijagnoze() With {.DDijag = txtDDijag.Text})
        txtDDijag.Text = ""

    End Sub
End Class




<XmlRoot(ElementName:="Ustanova")>
Public Class Ustanova
    <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:="Osiguranik")>
Public Class Osiguranik

    <XmlElement(ElementName:="Fil")>
    Public Property Fil() As String
        Get
            Return m_Fil
        End Get
        Set
            m_Fil = Value
        End Set
    End Property
    Private m_Fil As String
    <XmlElement(ElementName:="Isp")>
    Public Property Isp() As String
        Get
            Return m_Isp
        End Get
        Set
            m_Isp = Value
        End Set
    End Property
    Private m_Isp As String
    <XmlElement(ElementName:="Prez")>
    Public Property Prez() As String
        Get
            Return m_Prez
        End Get
        Set
            m_Prez = Value
        End Set
    End Property
    Private m_Prez As String

    <XmlElement(ElementName:="DodatneDijagnoze")>
    Public Property DodatneDijagnoze() As String
        Get
            Return m_DodatneDijagnoze
        End Get
        Set
            m_DodatneDijagnoze = Value
        End Set
    End Property
    Private m_DodatneDijagnoze As String

End Class

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

    <XmlElement(ElementName:="DDijag")>
    Public Property DDijag() As String
        Get
            Return m_DDijag
        End Get
        Set
            m_DDijag = Value
        End Set
    End Property
    Private m_DDijag As String

End Class




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

    Sub New()
        Me.Ustanova = New List(Of Ustanova)
        Me.Osiguranik = New List(Of Osiguranik)
    End Sub

    <XmlElement(ElementName:="Ustanova")>
    Public Property Ustanova() As List(Of Ustanova)
        Get
            Return m_Ustanova
        End Get
        Set
            m_Ustanova = Value
        End Set
    End Property
    Private m_Ustanova As List(Of Ustanova)



    <XmlElement(ElementName:="Osiguranik")>
    Public Property Osiguranik() As List(Of Osiguranik)
        Get
            Return m_Osiguranik
        End Get
        Set
            m_Osiguranik = Value
        End Set


    End Property
    Private m_Osiguranik As List(Of Osiguranik)


End Class

编辑:

这是下一个按钮:

faktura.Osiguranik.Add(New Osiguranik() With {.Fil = txtFil.Text, .Isp = txtIsp.Text, .Prez = txtPrez.Text, .DodatneDijagnoze = New DDijag() With {.DDijag = txtDDijag.Text}})

这是我制作 XML 的所有代码:(DodatneDijagnoze = 测试,DDijag = 工作)

<XmlRoot(ElementName:="Ustanova")>
Public Class Ustanova
    <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:="Osiguranik")>
Public Class Osiguranik

    <XmlElement(ElementName:="Fil")>
    Public Property Fil() As String
        Get
            Return m_Fil
        End Get
        Set
            m_Fil = Value
        End Set
    End Property
    Private m_Fil As String
    <XmlElement(ElementName:="Isp")>
    Public Property Isp() As String
        Get
            Return m_Isp
        End Get
        Set
            m_Isp = Value
        End Set
    End Property
    Private m_Isp As String
    <XmlElement(ElementName:="Prez")>
    Public Property Prez() As String
        Get
            Return m_Prez
        End Get
        Set
            m_Prez = Value
        End Set
    End Property
    Private m_Prez As String

    <XmlElement(ElementName:="DodatneDijagnoze")>
    Public Property DodatneDijagnoze() As String
        Get
            Return m_DodatneDijagnoze
        End Get
        Set
            m_DodatneDijagnoze = Value
        End Set
    End Property
    Private m_DodatneDijagnoze As String

End Class

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

    <XmlElement(ElementName:="DDijag")>
    Public Property DDijag() As String
        Get
            Return m_DDijag
        End Get
        Set
            m_DDijag = Value
        End Set
    End Property
    Private m_DDijag As String

End Class




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

    Sub New()
        Me.Ustanova = New List(Of Ustanova)
        Me.Osiguranik = New List(Of Osiguranik)
    End Sub

    <XmlElement(ElementName:="Ustanova")>
    Public Property Ustanova() As List(Of Ustanova)
        Get
            Return m_Ustanova
        End Get
        Set
            m_Ustanova = Value
        End Set
    End Property
    Private m_Ustanova As List(Of Ustanova)



    <XmlElement(ElementName:="Osiguranik")>
    Public Property Osiguranik() As List(Of Osiguranik)
        Get
            Return m_Osiguranik
        End Get
        Set
            m_Osiguranik = Value
        End Set


    End Property
    Private m_Osiguranik As List(Of Osiguranik)

出现错误 'DDijag' 类型的值无法转换为 'String'。


编辑:

我postapp在英文评论里

这是XML的样子:

<?xml version="1.0" encoding="utf-8"?>
<Invoice>
  <People>
    <Name>Test</Name>
    <LName>test</LName>
    <Age>42</Age>
  </People>
  <Family>
    <Sister>ttes</Sister>
    <Brother>ttsa</Brother>
    <F_Child>
      <SisterChild>dsads</SisterChild>
      <BrotherChild>sad</BrotherChild>
      <F_Child_Age>
        <SisterChildAge>fds</SisterChildAge>
        <BrotherChildAge>fdsfds</BrotherChildAge>
      </F_Child_Age>
    </F_Child>
  </Family>
</Invoice>

现在应用程序可以像这样无限输入 'Family':

<?xml version="1.0" encoding="utf-8"?>
<Invoice>
  <People>
    <Name>Test</Name>
    <LName>test</LName>
    <Age>42</Age>
  </People>
  <Family>
    <Sister>ttes</Sister>
    <Brother>ttsa</Brother>
    <F_Child>
      <SisterChild>dsads</SisterChild>
      <BrotherChild>sad</BrotherChild>
      <F_Child_Age>
        <SisterChildAge>fds</SisterChildAge>
        <BrotherChildAge>fdsfds</BrotherChildAge>
      </F_Child_Age>
    </F_Child>
  </Family>
  <Family>
    <Sister>dsa</Sister>
    <Brother>fds</Brother>
    <F_Child>
      <SisterChild>fds</SisterChild>
      <BrotherChild>fds</BrotherChild>
      <F_Child_Age>
        <SisterChildAge>fds</SisterChildAge>
        <BrotherChildAge>fds</BrotherChildAge>
      </F_Child_Age>
    </F_Child>
  </Family>
</Invoice>

我需要在现有 'Family' 的 'F_Child' 中进行无限制的输入 *需要看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<Invoice>
  <People>
    <Name>Test</Name>
    <LName>test</LName>
    <Age>42</Age>
  </People>
  <Family>
    <Sister>ttes</Sister>
    <Brother>ttsa</Brother>
    <F_Child>
      <SisterChild>dsads</SisterChild>
      <BrotherChild>sad</BrotherChild>
      <F_Child_Age>
        <SisterChildAge>fds</SisterChildAge>
        <BrotherChildAge>fdsfds</BrotherChildAge>
      </F_Child_Age>
    </F_Child>
    <F_Child>
      <SisterChild>hggs</SisterChild>
      <BrotherChild>sgfffd</BrotherChild>
      <F_Child_Age>
        <SisterChildAge>fgds</SisterChildAge>
        <BrotherChildAge>fdgsfds</BrotherChildAge>
      </F_Child_Age>
    </F_Child>
  </Family>
  <Family>
    <Sister>dsa</Sister>
    <Brother>fds</Brother>
    <F_Child>
      <SisterChild>fds</SisterChild>
      <BrotherChild>fds</BrotherChild>
      <F_Child_Age>
        <SisterChildAge>fds</SisterChildAge>
        <BrotherChildAge>fds</BrotherChildAge>
      </F_Child_Age>
    </F_Child>
  </Family>
</Invoice>

就我个人而言,我会像这样使用序列化......

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", .Test = New Job() With {.Job = Job.JobType.Yes}})
        invoice.Person.Add(New Person() With {.Age = 25, .LName = "Doe", .Name = "Jane", .Test = New Job() With {.Job = Job.JobType.No}})
        invoice.Person.Add(New Person() With {.Age = 55, .LName = "Doe", .Name = "Fred", .Test = New Job() With {.Job = Job.JobType.Yes}})
        invoice.Person.Add(New Person() With {.Age = 75, .LName = "Doe", .Name = "Harry", .Test = New Job() With {.Job = Job.JobType.No}})

        '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()

    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

    <XmlElement(ElementName:="Test")>
    Public Property Test() As Job
        Get
            Return _TEST
        End Get
        Set(ByVal value As Job)
            _TEST = value
        End Set
    End Property
    Private _TEST As Job
End Class

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

    Public Enum JobType
        Yes
        No
    End Enum


    Private _Job As JobType
    Public Property Job() As JobType
        Get
            Return _Job
        End Get
        Set(ByVal value As JobType)
            _Job = value
        End Set
    End Property
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

编辑

您需要为

的 selection 使用 ComboBox
<Job>Yes</Job> 

在窗体上放置一个 ComboBox1 控件并使用下面的代码将 JobType 枚举值显示为 select 从

Me.ComboBox1.DataSource = System.Enum.GetValues(GetType(Job.JobType))

然后你 'Next' 你会像这样使用按钮点击事件....

invoice.Person.Add(New Person() With {.Age = TextBox1.Text, .LName = TextBox2.Text, .Name = TextBox3.Text, .Test = New Job() With {.Job = CType(ComboBox1.SelectedValue, Job.JobType)}})

编辑字符串

<XmlRoot(ElementName:="Job")>
Public Class Job
    Private _Job As String

    Public Property Job() As String
        Get
            Return _Job
        End Get
        Set(ByVal value As String)
            _Job = value
        End Set
    End Property
End Class

并使用......

invoice.Person.Add(New Person() With {.Age = TextBox1.Text, .LName = TextBox2.Text, .Name = TextBox3.Text, .Test = New Job() With {.Job = "Yes"}})