VB.NET XML 序列化重复第三个子元素 [英] VB.NET XML Serialization Repeat third child Element

查看:46
本文介绍了VB.NET XML 序列化重复第三个子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序现在可以制作 XML,也可以制作 Element 'Osiguranik 的无限条目.

My app now can make XML and also can make unlimited entries of Element 'Osiguranik.

现在我想无限次输入元素Usluge",但是这个元素:Usluge"在元素:Osiguranik"中.

Now I want to make unlimited entries of element 'Usluge' but this Element: 'Usluge' is in Element: '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 的样子:

Here is XML how looks like:

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

我有 Element 按钮:'Osiguranik',我可以无限输入.

I have Button for Element: 'Osiguranik' and I can make ulimited entry.

我需要为 Element 制作 Button: 'Usluge' 以实现无限制进入.

I need to make Button for Element: 'Usluge' for unlimited entry.

推荐答案

好的,所以我认为您的代码中的类有问题,这使得修复非常困难,而且使用的是我不熟悉的语言....

OK so i think something was wrong with your Classes in your code, this was making it very difficult to fix plus also being in a language I am not familiar with....

所以我尝试整理您的代码.... 下面是一个将生成所需 XML 的示例.我只是在代码中创建了 Family\F_Child 以提高速度.你可能会发现自己的代码有些东西要改,或者用这段新代码作为模板重新开始

So I have tried to sort your code out.... Below is an example that will produce the required XML. I have simply created the Family\F_Child all in code for speed. You may find that you have to change some things in your own code, or use this new code as a template to start again

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

这篇关于VB.NET XML 序列化重复第三个子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆