【问题标题】:VB.NET XML Serialization Repeat third child ElementVB.NET XML 序列化重复第三个子元素
【发布时间】:2016-09-22 04:10:19
【问题描述】:

我的应用程序现在可以制作 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>

我有 Element: 'Osiguranik' 的按钮,我可以无限次进入。

我需要为 Element: 'Usluge' 制作无限制进入的按钮。

【问题讨论】:

    标签: xml vb.net serialization repeat


    【解决方案1】:

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

    所以我已经尝试将您的代码整理出来......下面是一个将生成所需 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
    

    【讨论】:

    • 我截图了你制作F_Child元素的代码问题:s33.postimg.org/r321y3en3/Problem.png
    • 确保 - Public Class F_Child.... 在代码中(从该屏幕截图中看不到)....
    • 此代码只能输入两个条目。您可以在我留下注释的按钮中简单地在我的项目中添加代码吗?请查看项目并在应有的位置添加代码。我浪费了两天时间来创建 xml,但又没有任何效果。项目:mediafire.com/download/hpv8u0kb96ptkuw/XML-Writer.zip
    • 代码可以添加任意数量的条目...您项目中的代码不正确,我没有时间修复...对不起。
    • 我项目中的代码是你上一个问题的代码。无论如何,谢谢你的帮助!
    猜你喜欢
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2019-12-21
    相关资源
    最近更新 更多