2016-05-24 16 views
0

私のアプリはXMLを作成できるようになり、Element 'Osiguranikも無制限に入力できます。VB.NET XMLシリアライゼーション3番目の子エレメントを繰り返します。

今、私は要素 'Usluge'を無制限に入力したいが、この要素: 'Usluge'はElement: 'Osiguranik'にある。 'Osiguranik' と私はulimitedエントリを作成することができます:私は要素のためのボタンがあり

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

:ここ

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です:

は、ここに私のコードです。

私は要素のためのボタンを作る必要があります:無制限の入力のための 'Usluge'。

答えて

1

OKので、私はそう私が試してみました

....これは修正することが非常に困難にプラスも私は精通していないです言語でされていた、何かがあなたのコードでは、あなたのクラスで間違っていたと思いますあなたのコードを並べ替える....以下は、必要なXMLを生成する例です。私は単純にスピードのためにすべてのコードでFamily \ F_Childを作成しました。ます。http://s33.postimgあなたは、私が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 
+0

を開始するためのテンプレートとしてこの新しいコードを使用することができます.org/r321y3en3/Problem.png –

+0

Public Class F_Child ....がコードに含まれていることを確認してください(そのスクリーンショットからは見ることができません)。 – Monty

+0

このコードは2つのエントリしか作成できません。メモを残すボタンでプロジェクトにコードを追加するだけですか?プロジェクトを見て、どこにコードを追加してください。私はXMLを作成する2日間を失ったし、もう一度何もする必要はありません。プロジェクト:mediafire.com/download/hpv8u0kb96ptkuw/XML-Writer.zip –

関連する問題