VB.NET XML序列化重复第三个子元素

时间:2016-05-24 19:22:35

标签: xml vb.net serialization repeat

我的应用程序现在可以制作XML,也可以无限制地输入Element'Osiguranik。

现在我想对元素'Usluge'进行无限制的输入,但是这个元素:'Usluge'在元素中:'Osiguranik'。

这是我的代码:

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

Public Class Form1

    Dim faktura As New Faktura

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'This button add Ustanova again
        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
        'Here I create XML
        Dim ns As New XmlSerializerNamespaces()
        Dim objStreamWriter As New StreamWriter("Invoice.xml") ' in the build folder
        Dim x As New XmlSerializer(faktura.GetType)

        ns.Add("", "")
        x.Serialize(objStreamWriter, faktura, ns)
        objStreamWriter.Close()

    End Sub

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

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' Here I want when click "Insert" to add Element 'Usluge' again.
    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 DDijag
        Get
            Return m_DodatneDijagnoze
        End Get
        Set(ByVal value As DDijag)
            m_DodatneDijagnoze = value
        End Set
    End Property
    Private m_DodatneDijagnoze As DDijag

End Class

<XmlRoot(ElementName:="DDijag")>
Public Class DDijag
    Private m_DDijag As String

    Public Property DDijag() As String
        Get
            Return m_DDijag
        End Get
        Set(ByVal value As String)
            m_DDijag = value
        End Set
    End Property

    <XmlElement(ElementName:="Usluge")>
    Public Property Usluge() As DatUsl
        Get
            Return m_Usluge
        End Get
        Set(ByVal value As DatUsl)
            m_Usluge = value
        End Set
    End Property
    Private m_Usluge As DatUsl


End Class


<XmlRoot(ElementName:="DatUsl")>
Public Class DatUsl
    Private m_DatUsl As String


    Public Property DatUsl() As String
        Get
            Return m_DatUsl
        End Get
        Set(ByVal value As String)
            m_DatUsl = value
        End Set
    End Property

    <XmlElement(ElementName:="SifUsl")>
    Public Property SifUsl() As String
        Get
            Return m_SifUsl
        End Get
        Set
            m_SifUsl = Value
        End Set
    End Property
    Private m_SifUsl 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

这是XML的样子:

<?xml version="1.0" encoding="utf-8"?>
<Faktura>
  <Ustanova>
    <Name>Test1</Name>
    <LName>Test3</LName>
    <Age>Test4</Age>
  </Ustanova>
  <Osiguranik>
    <Fil>Test3</Fil>
    <Isp>Test4</Isp>
    <Prez>Test5</Prez>
    <DodatneDijagnoze>
      <DDijag>50</DDijag>
      <Usluge>
        <DatUsl>1</DatUsl>
        <SifUsl>10</SifUsl>
      </Usluge>
    </DodatneDijagnoze>
  </Osiguranik>
</Faktura>

我有元素按钮:'Osiguranik',我可以进行极限输入。

我需要为元素制作Button:'Usluge'以便无限制地进入。

1 个答案:

答案 0 :(得分:1)

好的,所以我觉得你的代码中的Classes出了问题,这使得修复非常困难,加上我不熟悉的语言......

所以我试图对您的代码进行排序....下面是一个将生成所需XML的示例。我只是在代码中创建了Family \ F_Child以获得速度。您可能会发现必须在自己的代码中更改某些内容,或者使用此新代码作为模板重新开始

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
        Invoice.People.Age = 52
        Invoice.People.LName = "LName"
        Invoice.People.Name = "Name"

        Dim Family = New Family() With {.Brother = "brother", .Sister = "sister"}

        Dim F_Child1 = New F_Child() With {.BrotherChild = "BrotherChild1", .SisterChild = "SisterChild1", .F_Child_Age = New F_Child_Age() With {.BrotherChildAge = "BrotherChildAge1", .SisterChildAge = "SisterChildAge1"}}

        Family.F_Child.Add(F_Child1)

        Dim F_Child2 = New F_Child() With {.BrotherChild = "BrotherChild2", .SisterChild = "SisterChild2", .F_Child_Age = New F_Child_Age() With {.BrotherChildAge = "BrotherChildAge2", .SisterChildAge = "SisterChildAge2"}}

        Family.F_Child.Add(F_Child2)

        Invoice.Family.Add(Family)

        'Here I create XML
        Dim ns As New XmlSerializerNamespaces()
        Dim objStreamWriter As New StreamWriter("Invoice.xml") ' in the build folder
        Dim x As New XmlSerializer(Invoice.GetType)

        ns.Add("", "")
        x.Serialize(objStreamWriter, Invoice, ns)
        objStreamWriter.Close()

        MsgBox("XML is made.")

    End Sub
End Class

<XmlRoot(ElementName:="People")>
Public Class People
    <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 Integer
        Get
            Return m_Age
        End Get
        Set
            m_Age = Value
        End Set
    End Property
    Private m_Age As Integer
End Class

<XmlRoot(ElementName:="F_Child_Age")>
Public Class F_Child_Age
    <XmlElement(ElementName:="SisterChildAge")>
    Public Property SisterChildAge() As String
        Get
            Return m_SisterChildAge
        End Get
        Set
            m_SisterChildAge = Value
        End Set
    End Property
    Private m_SisterChildAge As String
    <XmlElement(ElementName:="BrotherChildAge")>
    Public Property BrotherChildAge() As String
        Get
            Return m_BrotherChildAge
        End Get
        Set
            m_BrotherChildAge = Value
        End Set
    End Property
    Private m_BrotherChildAge As String
End Class

<XmlRoot(ElementName:="F_Child")>
Public Class F_Child
    <XmlElement(ElementName:="SisterChild")>
    Public Property SisterChild() As String
        Get
            Return m_SisterChild
        End Get
        Set
            m_SisterChild = Value
        End Set
    End Property
    Private m_SisterChild As String
    <XmlElement(ElementName:="BrotherChild")>
    Public Property BrotherChild() As String
        Get
            Return m_BrotherChild
        End Get
        Set
            m_BrotherChild = Value
        End Set
    End Property
    Private m_BrotherChild As String
    <XmlElement(ElementName:="F_Child_Age")>
    Public Property F_Child_Age() As F_Child_Age
        Get
            Return m_F_Child_Age
        End Get
        Set
            m_F_Child_Age = Value
        End Set
    End Property
    Private m_F_Child_Age As F_Child_Age
End Class

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

    Sub New()
        Me.F_Child = New List(Of F_Child)
    End Sub


    <XmlElement(ElementName:="Sister")>
    Public Property Sister() As String
        Get
            Return m_Sister
        End Get
        Set
            m_Sister = Value
        End Set
    End Property
    Private m_Sister As String
    <XmlElement(ElementName:="Brother")>
    Public Property Brother() As String
        Get
            Return m_Brother
        End Get
        Set
            m_Brother = Value
        End Set
    End Property
    Private m_Brother As String
    <XmlElement(ElementName:="F_Child")>
    Public Property F_Child() As List(Of F_Child)
        Get
            Return m_F_Child
        End Get
        Set
            m_F_Child = Value
        End Set
    End Property
    Private m_F_Child As List(Of F_Child)
End Class

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

    Sub New()
        Me.Family = New List(Of Family)
        Me.People = New People()
    End Sub

    <XmlElement(ElementName:="People")>
    Public Property People() As People
        Get
            Return m_People
        End Get
        Set
            m_People = Value
        End Set
    End Property
    Private m_People As People
    <XmlElement(ElementName:="Family")>
    Public Property Family() As List(Of Family)
        Get
            Return m_Family
        End Get
        Set
            m_Family = Value
        End Set
    End Property
    Private m_Family As List(Of Family)
End Class