vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Sunday, March 4, 2012

Reading and writing XML data in asp.net


Introduction:

It’s very interesting when write about XML  an extensible markup language…

Here you are going to see different way to read and write the XML data.
Now I share the simple and easy methods to know about xml read and write.
Its basic question initially to ask about an xml everywhere

What is XML and why use xml?

·         Xml is the universal format for describing and exchange the data.
·         Xml  is self describing and easy to work
·         Xml described how the data is structured.

Read XML Data
By using dataset we can easily read the xml data and easy bind the data in gridview or in dataset
  Dim ds As New DataSet
        ds.ReadXml(Server.MapPath("prod.xml"))

Once we get the data to dataset we can manipulate the data to our needs..
For Each dr As DataRow In ds.Tables(0).Rows
      
 Next

Once the data has set in dataset then we easily bind the data into gridview or in dataset
        grddet.DataSource = ds
        grddet.DataBind()

Write XML data
By using dataset this is way to write XML data.
Dim sqlda As New SqlDataAdapter("select * from products", sqlcon)
        sqlda.Fill(ds, "product")
        ds.DataSetName = "Newproducts"
        ds.WriteXml(Server.MapPath("prod1.xml"))

 Also using xmldocument we can get the data from dataset and we do manipulate or bind the data if the conditions needed..
Dim xmldoc As New XmlDataDocument(ds)
        grddet.DataSource = xmldoc.DataSet
        grddet.DataBind()


For doing manipulation
'Manupulate
        Dim xele As XmlElement
        xmldoc.GetElementsByTagName("products")
        For Each xele In xmldoc.ChildNodes
            If xele.Name = "car" Then
                xele.Value = "100"
            End If
        Next


Conclusion:
From this article you have seen how to read and write XML ,I hope you learn something from this keep reading happy coding

0 comments:

Post a Comment