Monday, March 21, 2005

How To Read/Write Into XML

Private sub WriteXML()

Try

Dim doc As

XmlDocument

doc = New XmlDocument()

'Chk whether xml files is exists or not

Try

doc.Load(Server.MapPath("book.xml"))

Catch ex As

Exception

doc.LoadXml("<Books></Books>")

End

Try

Dim node As XmlElement, pNode As

XmlElement

pNode = doc.CreateElement("Book")

doc.FirstChild.AppendChild(pNode)

node = doc.CreateElement("Title")

node.InnerText = txtTitle.Text

pNode.AppendChild(node)

node = doc.CreateElement("Author")

node.InnerText = txtAuthor.Text

pNode.AppendChild(node)

node = doc.CreateElement("Year")

node.InnerText = txtYear.Text

pNode.AppendChild(node)

'save the output to a file

doc.Save(Server.MapPath("book.xml"))

ReadXml()

Catch Eh As

Exception

Response.Write("Exception: {0}" & Eh.ToString())

End

Try

End

Sub

Private Sub

ReadXml()

Try

Dim myDataSet As New

DataSet()

myDataSet.ReadXml(Server.MapPath("book.xml"))

dgBooksPretty.DataSource = myDataSet

dgBooksPretty.DataBind()

Catch ex As

Exception

Response.Write("Exception: {0}" & ex.ToString())

End

Try

Note: some more way of Reading / Writting XML using C#.net

http://riteshk.blogspot.com/2007/07/how-to-readwrite-xml-string-in.html

No comments: