asp.net Working with XML-Write,Read with dataset

asp.net Working with XML


Imports System.Xml

1 ################## Creating xml file ##################

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xw As XmlTextWriter = New XmlTextWriter(Server.MapPath("vin.xml"), System.Text.Encoding.ASCII)
        xw.WriteStartDocument(True)
        xw.WriteStartElement("vin")
        xw.WriteElementString("name", "vinod")
        xw.WriteElementString("eid", "009383")

        xw.WriteEndElement()
        xw.WriteEndDocument()
        xw.Close()

    End Sub


2 ######################## Reading xml File ###############
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim xr As XmlTextReader = New XmlTextReader(Server.MapPath("vin.xml"))
        While (xr.Read)
            Response.Write(xr.ReadString)

        End While
                    xr.close()
    End Sub

3 #################### Loading xml data to gridview ############

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim ds = New DataSet
        ds.ReadXml(Server.MapPath("vin.xml"))

        GridView1.DataSource = ds.Tables(0)
        GridView1.DataBind()




    End Sub


4. ########################## Creating and Writing Dataset to xml File  ###########


Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim ds As New DataSet
        Dim dt As New DataTable
        dt.Columns.Add("eid", Type.GetType("System.String"))
        dt.Columns.Add("name", Type.GetType("System.String"))
        Dim dr As DataRow = dt.NewRow
        dr(0) = "666"
        dr(1) = "pinx"
        dt.Rows.Add(dr)
        ds.tables.add(dt)
        ds.WriteXml(Server.MapPath("vin.xml"))



    End Sub


Comments