For Answers, see/post comments

Create xml file

This sample code shows how to generate XML and write to a file.

Imports System.Xml
Imports System.IO
Imports System.Data.SqlClient

Public Function CreateXML() As Boolean
Dim objDataSet As DataSet
Dim intI As Integer
Dim strPath As String
Dim objWriter As XmlTextWriter
'create an instance of the XmlTextWriter object

Try
' location to the XML file to write
strPath = strFilePath & "\XML\" & strSessionID & ".xml"
objWriter = New XmlTextWriter(strPath, System.Text.Encoding.Default)
' start writing the XML document
objWriter.WriteStartDocument()
' write a comment in our XML file
objWriter.WriteComment("Employee")
' starting with the root element i.e. "movies"
objWriter.WriteStartElement("Menu")
'Create dataset as per your requirement.Here xml for Employee is created.The elements are Empno,Emp_name and salary
'Set data to XML tags


If objDataSet.Tables(0).Rows.Count > 0 Then
For intI = 0 To objDataSet.Tables(0).Rows.Count - 1
objWriter.WriteStartElement("Employee") ' output the "Employee" element
objWriter.WriteElementString("EmpNo", objDataSet.Tables(0).Rows(intI).Item("EmpNo").ToString)
objWriter.WriteElementString("Name", objDataSet.Tables(0).Rows(intI).Item("Emp_Name").ToString)
objWriter.WriteElementString("Salary", objDataSet.Tables(0).Rows(intI).Item("Salary"))
objWriter.WriteEndElement() ' close "Employee" element
Next
End If
' end the "Menu" element
objWriter.WriteEndElement()
' flush and write XML data to the file
objWriter.Flush()
Return True

Catch ex As Exception
Return False
Finally
' clear up memory
objWriter.Close()
objWriter = Nothing
objDataSet = Nothing
End Try
End Function

No comments: