0

I need to write an xml file with two starting elements how can I do this?

I have tried the following but to no avail.

Dim xmldoc As XmlDocument = New XmlDocument()
                xmldoc.Load(IO.Directory.GetCurrentDirectory & "\Projects.xml")

                With xmldoc.SelectSingleNode("/COMPELATION").CreateNavigator().AppendChild()
                    .WriteStartElement("DATA")
                    .WriteStartElement("ID")
                    .WriteElementString("PROJECT", TextBox1.Text)
                    .WriteElementString("DESCRIPTION", TextBox2.Text)
                    .WriteElementString("STATUS", ComboBox1.Text)
                    .WriteElementString("OWNER", TextBox4.Text)
                    .WriteElementString("DATE", TextBox5.Text)
                    .WriteElementString("CLIENT", TextBox6.Text)
                    .WriteEndElement()
                    .WriteEndElement()
                    .Close()
                End With

The desierd output would be as follows:


123 my description Open Shop assistant jack 28-08-2015 Toms store 123 my description Open Shop assistant jack 28-08-2015 Toms store

LabRat
  • 1,996
  • 11
  • 56
  • 91

1 Answers1

0

EDIT! THere are several way to do:

 VB.NET program that uses XmlWriter

 Imports System.Xml

 Module XmlModule
     ''' <summary>
     ''' Employee type.
     ''' </summary>
Class Employee
Public Sub New(ByVal id As Integer, ByVal firstName As String, _
           ByVal lastName As String, ByVal salary As Integer)
    ' Set fields.
    Me._id = id
    Me._firstName = firstName
    Me._lastName = lastName
    Me._salary = salary
End Sub

' Storage of employee data.
Public _firstName As String
Public _id As Integer
Public _lastName As String
Public _salary As Integer
End Class

Sub Main()
' Create array of employees.
Dim employees(2) As Employee
employees(0) = New Employee(1, "Prakash", "Rangan", 70000)
employees(1) = New Employee(5, "Norah", "Miller", 21000)
employees(2) = New Employee(17, "Cecil", "Walker", 60000)

' Create XmlWriterSettings.
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True

' Create XmlWriter.
 Using writer As XmlWriter = XmlWriter.Create("C:\employees.xml", settings)


    ' Loop over employees in array.
    Dim employee As Employee

  ' Begin writing.
    writer.WriteStartDocument()
    writer.WriteStartElement("DATA") ' Root.

    For Each employee In employees
    writer.WriteStartElement("ID")
           Writer.WriteElementString("FirstName", employee._firstName)
           writer.WriteElementString("LastName", employee._lastName)
           writer.WriteElementString("Salary", employee._salary.ToString)
         writer.WriteEndElement()

    writer.WriteEndElement()
    Next
' End document.
    writer.WriteEndDocument()
End Using
End Sub
 End Module

the OUTPUT

Output (Written to a file on the disk.)

 <?xml version="1.0" encoding="utf-8"?>

 <DATA>
   <ID>
     <FirstName>Prakash</FirstName>
     <LastName>Rangan</LastName>
     <Salary>70000</Salary>
  </ID>
   <DATA>
     <ID>
     <FirstName>Norah</FirstName>
     <LastName>Miller</LastName>
     <Salary>21000</Salary>
   </ID>
   </DATA>
   <DATA>
     <ID>
     <FirstName>Cecil</FirstName>
     <LastName>Walker</LastName>
     <Salary>60000</Salary>
   </ID>
 </DATA>

I think is exacly what you want

 <DATA>
   <ID>
      <PROJECT>123</PROJECT>


 <Employees>
   <Employee>
     <ID>1</ID>

Have fun codding :) CristiC777

CristiC777
  • 481
  • 11
  • 20
  • No I realy want, {} – LabRat Sep 07 '15 at 09:55
  • No but I see what you have sugested its just not the answer I'm looking for... your code works but its not the answer to my situation . Your code adds a child tag thats not a problem. I need to have two parent tags before the children apear – LabRat Sep 07 '15 at 10:49
  • Try to play now ;) have a nice day – CristiC777 Sep 07 '15 at 11:04