Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys,

here's what I have done:
In my Command Button to execute the Process:
VB
PopulateTables()
GetList_First()
MessageBox.Show("XML File Successfully Generated !", "XML Creation", MessageBoxButtons.OK, MessageBoxIcon.Information)


In the PopulateTables() Function:
VB
Public Sub PopulateTables()
        Dim strSELECT As String = "SELECT * FROM tblstudents "

        Dim DReader As New DataClassReader
        DReader.strSELECTSQL = strSELECT
        DReader.ReadData()
        dtgView.Rows.Clear()
        Dim a As Integer = 0

        Do While DReader.PinoyLDataReader.Read
            dtgView.Rows.Add()
            With DReader.PinoyLDataReader
                dtgView.Item(0, a).Value = .Item("idtblstudents")
                dtgView.Item(1, a).Value = .Item("studentID")
                dtgView.Item(2, a).Value = .Item("s_sy")
                dtgView.Item(3, a).Value = .Item("fullname")
                dtgView.Item(4, a).Value = .Item("fname")
                dtgView.Item(5, a).Value = .Item("lname")
                dtgView.Item(6, a).Value = .Item("mname")
                dtgView.Item(7, a).Value = .Item("bday")
                dtgView.Item(8, a).Value = .Item("gender")
                dtgView.Item(9, a).Value = .Item("address")
                dtgView.Item(10, a).Value = .Item("s_contctno")
                dtgView.Item(11, a).Value = .Item("parent_guardian")
                dtgView.Item(12, a).Value = .Item("p_contctno")
                dtgView.Item(13, a).Value = .Item("s_year_section")
                dtgView.Item(14, a).Value = .Item("remarks")



            End With
            a = a + 1
        Loop
        DReader.PinoyLDataReader.Close()
    End Sub


In the GetList_First() Function:
VB
Public Sub GetList_First()
        If dtgView.Rows.Count > 0 Then
            Dim i As Integer = 0
            Dim rCnt As Integer = dtgView.Rows.Count
            For i = 0 To rCnt - 1
                With dtgView
                    Dim writer As New XmlTextWriter("Student List.xml", System.Text.Encoding.UTF8)
                    writer.WriteStartDocument(True)
                    writer.Formatting = Formatting.Indented
                    writer.Indentation = 2
                    writer.WriteStartElement("Student")
                    Create_XML(.Item(0, i).Value, _
                               .Item(1, i).Value, _
                               .Item(2, i).Value, _
                               .Item(3, i).Value, _
                               .Item(7, i).Value, _
                               .Item(8, i).Value, _
                               .Item(9, i).Value, _
                               .Item(10, i).Value, _
                               .Item(11, i).Value, _
                               .Item(13, i).Value, _
                               writer)
                    writer.WriteEndElement()
                    writer.WriteEndDocument()
                    writer.Close()
                End With
            Next
        End If
    End Sub


In the Create_XML() Function :
VB
Private Sub Create_XML(ByVal idtblstudents As String, _
                          ByVal studentID As String, _
                          ByVal s_sy As String, _
                          ByVal fullname As String, _
                          ByVal bday As String, _
                          ByVal gender As String, _
                          ByVal address As String, _
                          ByVal s_contctno As String, _
                          ByVal parent_guardian As String, _
                          ByVal s_year_section As String, _
                          ByVal writer As XmlTextWriter)


       writer.WriteStartElement("Students")
       writer.WriteStartElement("idtblstudents")
       writer.WriteString(idtblstudents)
       writer.WriteEndElement()
       writer.WriteStartElement("studentID")
       writer.WriteString(studentID)
       writer.WriteEndElement()
       writer.WriteStartElement("s_sy")
       writer.WriteString(s_sy)
       writer.WriteEndElement()
       writer.WriteStartElement("fullname")
       writer.WriteString(fullname)
       writer.WriteEndElement()
       writer.WriteStartElement("bday")
       writer.WriteString(bday)
       writer.WriteEndElement()
       writer.WriteStartElement("gender")
       writer.WriteString(gender)
       writer.WriteEndElement()
       writer.WriteStartElement("address")
       writer.WriteString(address)
       writer.WriteEndElement()
       writer.WriteStartElement("s_contctno")
       writer.WriteString(s_contctno)
       writer.WriteEndElement()
       writer.WriteStartElement("parent_guardian")
       writer.WriteString(parent_guardian)
       writer.WriteEndElement()
       writer.WriteStartElement("s_year_section")
       writer.WriteString(s_year_section)
       writer.WriteEndElement()

       writer.WriteEndElement()
   End Sub


It performs correctly, but the problem is it only gets the last record/data in the Datagrid...

Please help...

Thanks in advance..... ;)
Posted

1 solution

You need to have a writer.WriterStartElemet() and writer.WriterEndElement() outside of the CreateXML i.e. somewhere outside the loop that runs till rCnt. This parent element will then contain all the student xmls.
 
Share this answer
 
Comments
jleonorlane 28-Nov-10 6:27am    
..but it does not work....
Abhinav S 28-Nov-10 7:16am    
You need to move that + the Dim writer As New XmlTextWriter("Student List.xml", System.Text.Encoding.UTF8 bit outside the for loop.
jleonorlane 28-Nov-10 8:54am    
i got an error in the Creat_XML() Functions, says "Token StartElement in state Epilog would result in an invalid XML document."

My Codes in the GetList_First() Function:
.....
Dim writer As New XmlTextWriter("Student List.xml", System.Text.Encoding.UTF8)
writer.WriteStartDocument(True)
writer.Formatting = Formatting.Indented
writer.Indentation = 2

writer.WriteStartElement("Student")
writer.WriteEndElement()

Dim i As Integer = 0
Dim rCnt As Integer = dtgView.Rows.Count
For i = 0 To rCnt - 1

With dtgView

Create_XML(.Item(0, i).Value, _

.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900