.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Creating XML File

Posted By:Athira Sreejith       Posted Date: February 05, 2009    Points: 10    Category: File Operation    URL: http://www.dotnetspark.com  
 

This is coding for create the XML FILE in VB.Net. This code will generate XML file dynamically. Code used VB.Net as Language

Imports System.IO

Imports System.Xml

Private Sub generateXML()

Dim filename1, filename2 As String

----------------------------------------------------------------

Checking that whether the directory is available already

----------------------------------------------------------------

If Not Directory.Exists("C:\ServerNewLeads") Then

Directory.CreateDirectory("C:\ServerNewLeads")

End If


Dim rd As OleDbDataReader

Dim sql, sql1 As String

Dim cmd, cmd1 As OleDbCommand


If objconnection.State = ConnectionState.Closed Then

objconnection.Open()

End If

sql1 = "select * from LMS_QualifiedLead where QualifiedLeedId=(" & "select max(QualifiedLeedId) from LMS_QualifiedLead )"

cmd1 = New OleDbCommand(sql1, objconnection)


Dim leadid As String

leadid = cmd1.ExecuteScalar

objconnection.Close()

-------------------------------------------------------------

Creating the File Name with the Extension .xml

First corresponding path then file name with the extension .xml

--------------------------------------------------------------

filename1 = "C:\ServerNewLeads\" & "NewLead" & leadid & ".xml"

--------------------------------------------------------------

Checking whether the newly created filename is already is

Available. If filename is already available deleting the filename.

--------------------------------------------------------------

If file.Exists(filename1) Then

file.Delete(filename1)

End If

---------------------------------------------------------------

creating a variable as mywriter to write the xml format value into

this variable.

---------------------------------------------------------------

Dim mywriter As XmlTextWriter

mywriter = New XmlTextWriter(filename1, Nothing)

--------------------------------------------------

To create the following syntax method in the XML

The following coding is written "WriteProcessingInstruction"

---------------------------------------------------

mywriter.WriteProcessingInstruction("xml", "version='1.0' encoding='utf-8'")

--------------------------------------------------------------

Binding the values one by one with the help of With..End With

--------------------------------------------------------------

With mywriter

---------------------------------------------------------

.WriteStartElement is used to create the heading for XML

----------------------------------------------------------

.WriteStartElement("NewLeads")

sql = "select * from LMS_QualifiedLead where QualifiedLeedId=(" & "select max(QualifiedLeedId) from LMS_QualifiedLead )"

cmd = New OleDbCommand(sql, objconnection)

If objconnection.State = ConnectionState.Closed Then

objconnection.Open()

End If

rd = cmd.ExecuteReader

While rd.Read()

---------------------------------------------------------------

.WriteElementString is used to bind the value one by one

---------------------------------------------------------------------


.WriteElementString("QualifiedLeedId", rd(0))

.WriteElementString("CompanyName", rd(1))

.WriteElementString("CountryId", rd(2))

.WriteElementString("ContactFirstName", rd(3))

.WriteElementString("ContactLasrName", rd(4))

.WriteElementString("UserTypeId", rd(5))

.WriteElementString("EmailId", rd(6))

.WriteElementString("PhoneCC", rd(7))

.WriteElementString("PhoneAC", rd(8))

.WriteElementString("PhoneNo", rd(9))

.WriteElementString("BussRelId", rd(10))

.WriteElementString("Remarks", rd(11))

.WriteElementString("CustResponseId", rd(12))

.WriteElementString("LMeMail", rd(13))

.WriteElementString("LMTelesales", rd(14))

.WriteElementString("LMTeleMarketing", rd(15))

.WriteElementString("LMRoadShow", rd(16))

.WriteElementString("LMRadio", rd(17))

.WriteElementString("LMTelevision", rd(18))

.WriteElementString("LMPrintMedia", rd(19))

.WriteElementString("LMSeminar", rd(20))

.WriteElementString("LMStaff", rd(21))

.WriteElementString("LMWebsite", rd(22))

.WriteElementString("ProductId", rd(23))

.WriteEndElement()

End While

mywriter.Flush()

mywriter.Close()

End With

objconnection.Close()

End Sub


FileName Creating exactly

Corresponding Exact Path.

Created XML File Sample Format

Corresponding Path with the filename



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend